MVC :: Validate A List Box?
Jan 20, 2011list box is of this kind
<%=Html.ListBox("emp", (SelectList)ViewData["Categories"], new { onchange = "addItem();", style = "width: 155px; height: 35px;" })%>
how to do with cilent validation
list box is of this kind
<%=Html.ListBox("emp", (SelectList)ViewData["Categories"], new { onchange = "addItem();", style = "width: 155px; height: 35px;" })%>
how to do with cilent validation
Let's say we have a entity to add - an Invoice with multiple Lines
public class Lines
{
public string ObjectName;
public string Color;
}
public class Invoice
{
public string DocumentID;
public DateTime InvoiceDate;
public List<Lines> lines;
public string Observation;
}
The Lines is valid if and only if it has ObjectName and Color
Now, when in AddMode , the requirements are to show enter a new Invoice with 10 Lines by default .
This was easy (binding to an object that contains list, right?) - but in validation mode, the ModelState.IsValid is validating all my 10 lines - regardless if the user have been entered only 2 lines.
I have tried to remove the empty lines by writing a Binder for Invoice, inheriting from DefaultModelBinder and removing the empty lines ( those for ObjectName AND Color both nulls or empty ) .But, no matter what overriding method (CreateModel , BindModel ) I have used , no success. The binding model occurs first.
I have tried to do ModelState.Clear() and ValidateModel(invoice) in the controller. But ValidateModel is not validating "sub-properties of complex properties" - see comments from
[URL]
If you know how to either :
a) re-validate all properties (including for complex object)
b) modifying the model before ModelState grab errors
c) ?
(I can not debug with MVC2 source code - but this is another post...)
Data annotation to validate an inbound model in MVC:
public class ValidNumber
{
[RegularExpression(@"^d+$", ErrorMessage = "*")]
public string number { get; set; }
}
Would I need to create my own class to validate a List<string> or can I do something like this? What code could I write in C# to add a Regex validator for a list of string?
public class ValidNumberList
{
[RegularExpression(@"^d+$", ErrorMessage = "*")]
public List<string> numbers { get; set; }
}
I want to validate checkbox list, i am using this strategy which is mention in this lik given below
http://www.4guysfromrolla.com/webtech/tips/t040302-1.shtml
but i am using c#, and here they use script language ="vb" . so anyone tell me , How can I validate checkbox list using c#
n my aspx page i am having a checkbox list ..It has binded values from a table..
I need to validate the checkbox list ..I tried the following script
var checkBoxCount = 0;
var elements = document.getElementById('<%=ChkBoxList.ClientID%>');
for(i=0; i<elements.length;i++)
{
if(elements[i].checked)
[code]....
How to validate dropdown list using javascript?
View 4 Repliesi have a dropdown list and a sql datasourse.the list has 3 items(Please select, Item 1 Item2)i need to validate that item1 or 2 is selected.i tryed this
http://forums.asp.net/t/1106917.aspx
but it did not work, it does validate all the time,
I am using javascript to validate user input on my aspx page. I am easily able to validate textboxes and dropdown list for differenet scenarios.
Now, on one of my dropdown lists (for country), I need to check if it is an allowed country or not for a particular service. I have stored the valid country list in a static property. Is there a way to validate my dropdownlist selected value against that static property?
function validateService(source, args)
{
var country = document.getElementById('<%= ddDestCountry.ClientID %>');
var service = document.getElementById('<%= ddService.ClientID %>');
// Get allowed country list from my static class
var countryList = document.getElementById('<%= StaticProperties.EUCountryList %>');
if (service.value == "P")
{
// I want to do something like this
if (!countrylist.Contains(country.value))
{
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
return;
}
Update(Additional Information): The static property is read-only so it cannot be tampered with from the page.
How to validate DropDownCheckBoxes control at client side(javascript or Jquery)? I would like to restrict the user to submit the page if no item is selected in the dropdowncheckboxes control. i.e no check box is checked.Javascript/Jquery code to validate the control at client side.
View 1 Repliesi have a drop down list and a text box, inside a listview row (and this is a repeating layout). i populate each dropdownlist with same contents ( these contents/items come from a custom collection object).
i have added a required field validator in the listview's item template, that has its validation group = the validation group of the drop down list, and validates if the textbox is blank or not. this validation takes place on the selected index changed of the drop downlist for that row.
1. on selected index changed, EACH dropdownlist validates only its corressponding textbox perfectly (which is what i want)
but if i enter some text in one of the textboxes after doing 1, the dropdownlist for THAT particular row, assigns the correct value to the text entered. but for other rows (for which i changed their corresponding ddls to test the required field validation and for which the text boxes are still blank), their ddls by itself assign the values to the blank text without validating.
markup of code for listview is [Code]....
code behind for the ddl selected index changed is [Code]....
method GetIndexFromId retrieves index of the custom object inside a collection depending on the Id (which is a property) of the object.
i notice that the ddl selectedindexchanged is being hit as many number of times as many rows i change (i.e as many ddls in different rows i change to check the required field validation)
I'm trying out the jQuery Validation plugin jQuery Docs Here is the markup of my form:
<% using (Html.BeginForm("action", "contoller", null, FormMethod.Post, new { id = "sxform" })){%>
<div id="manifest">
Manifest Option:<br />
<%= Html.DropDownList("docid", ViewData["manifests"] as SelectList, new { @class = "required" })%>
</div>
<div id="release">
Release Version:<br />
<%= Html.TextBox("release", null, new { @class = "required" })%>
</div>
<div id="locale">
Localization:<br />
<%= Html.DropDownList("localization", ViewData["localizations"] as SelectList, new { @class = "required" })%>
</div>
<div id="label">
Label:<br />
<%= Html.TextBox("label", null, new { @class = "required" })%>
</div>
<div id="session">
Session ID (optional):<br />
<%= Html.TextBox("sessionInput", null, new { @class = "required" })%>
</div>
<div id="submit"><input type="submit" value="Build" /></div>
<% } %>
JS:
$(document).ready(function(){
$("#sxform").validate();
});
I am using MS MVC HTML Helpers to render this form. The resulting markup looks fine. IE each input and selection element contains the attribute 'class' with the value 'required'. When I submit this form the validation does noting.
I would like to know how to use jQuery's Validate plugin to add rules to validate controls in ASCX custom user control?
View 4 RepliesHow can i generate dropdown lists based on what has been selected in the checckbox list. Below is an example of what i need. if the user selects the options day, lotID and waferID, then 3 cascading dropdown lists should be displayed. And then a gridview displays data based on what has been chosen in the dropdown lists.
Day
LotID
SlotID
WaferID
VendorID
ToolID
LocationDetected
ProcessStep
Stage
Precipe
WaferStartMaterial
WaferStartVendor
WaferStartLot
WaferDiameterCOA
WaferMapTitle
BreakPoint
BreakpointSide
BreakpointMeasurement
I have drop down list box that I would like to show two columns, is this possible? Or can it be done with a list box?The query correctly retrieves all of the table data to be displayed, one colum is an abbreviation, the other a bit longer description.
View 2 RepliesI have a situation where I want to show the selected records out of total records for a product of an employee
1) There is a checkbox list bind to <List> of objects from object datasource (For total items in list)
2) Now I want to check the selected items for a particular record in this list
3) For this purpose I have another list of <List> selected items returned by data access layer (For selected items for that employee )
4) How do I bind the selected objects with the total items list ?
5) In spaghetti coding model it was all too easy just by binding the checkbox list with a sql data source and running a for each on form load
Just wondering how to implement a user's list into website ? im using ASP.NET C#. I want to be able to:
add/delete usersadd/remove roles
Clicking the label of a drop down list, re-sets the selected value to the first item in the list. This has a history of causing data entry problems when users accidently click on the label.
In the below example ddlContactType has a selected value as 'PRIMARY' and whenever i click on the lblContactType Text, the dropdown box Selected value is changing to "Select One" Text.
How to prevent this one. It should be on PRIMARY Only.
<p>
<tr> <td ><asp:Label ID="lblContactType" Font-Bold="true" Text="Contract Type: *" ForeColor= "red" runat="server" AssociatedControlID="ddlContactType"></asp:Label> </td> <td > <asp:DropDownList runat="server" ID="ddlContactType" DataSourceID="SqlDataSource2" AppendDataBoundItems="true"
DataTextField="TYPE" DataValueField="ID" SelectedValue='<%# Bind("CONTACT_TYPE_ID") %>'>
<asp:ListItem Text="Select One" Value="0" Selected="True"></asp:ListItem>.......
I have a databound dropdownlist and have a problem with the default value it selects.When I attempt to edit a record the dropdownlist shows the first value in the list rather than the current value. I want the dropdown list to display the current value.
I'm sure this is very simple but I'm very new to ASP.NET.
I'm using .NET version 4.
I have the following Students class:
[Code]....
I need the 1 since it's a foreign key in another table. For the life of me, I can't get this to work like this.
I'm trying to progam a strongly typed "Create" view using MVC 3 and razor. I want the user to be able to select a customer from a dropdown list (I populated this from my database using ViewBag in the controller). When the user has selected a customer I want a separate dropdown list to generate a list of dogs belonging to that customer related by the customerID in the database. The create button on the form will then take both of these values along with the the other fields and save it to the database.
View 1 RepliesI have a requirement in my application that, while saving the application, need to get all the value from drop down list and pass it to the view model. But application should not allow the user to select more than one item from drop down list manually, ie, only one value at a time. My view model is like ...
public class ListManagement
{
public IEnumerable<selectListItem> InactiveProduct { get; set; }
public string[] InactiveProductSelected { get; set; }
public IEnumerable<selectListItem> ActiveProduct { get; set; }
}
[code]...
1 My DataTable Return following Data
Acct Books Annual
Acct Books Monthly
Acct Tax Income Tax
Admin GH Eqpt
Admin Offc Eqpt
Admin Offc Misc
Admin Offc Eqpt
[code]...
I am using a strongly-typed view to display a list of products, where every li-element gets a unique id:
<ul id="product-list">
<% foreach (var item in Model.Products)
{ %>
<li <%= "id="product_" + item.Id + """ %> >
<div class="item">
<%= item.Name %>
</div>
</li>
<% } %>
</ul>
Now I want to attach to the click-event of every single li-element, so that if the user clicks on a div-element, detailed product-informationen should be loaded asynchronously into a details-pane. I know how I can use jQuery to invoke an action-method ajax-style and also how to display the json-result which contains the product-details, BUT I have no idea, how I could attach the onclick-event to every single div, so that I can use the productId to load the details.
How would I create a list then on click add data to the list which is then stored into a session variable, then on another page retrieve the variable and output them? Currently I have:
Page Load:
List <myClass> listName=new List<myClass>
OnClick:
listName.add(3);
listName.add(4);
Session["IDs"]=listName;
Second Page
I need to get the whole list and add output the list so I can output (hopefully) the '3' and '4'.
Is it possible to set tab index value for list items of ASP.Net RadioButtonList control.
View 1 Replies