jquery - ASP.Net MVC 3 unobtrusive client validation does not work with drop down lists -


i have simple drop down list, first item in list has empty value. if not select in list client validation ignores it. have field set required on model using annotation attributes.

 @html.dropdownlistfor(model => model.ccpayment.state, unitedstatesstates.stateselectlist)    [required(errormessage = "state required.")]     public string state     {                 {             return _state;         }         set         {             _state = value;         }     } 

any ideas? missing something?

it looks legitimate bug, here's best workaround i've found in search:

http://forums.asp.net/t/1649193.aspx

in short. wrap source of problem, dropdownlistfor, in custom html extension , manually retrieve unobtrusive clientside validation rules this:

idictionary<string, object> validationattributes = htmlhelper.     getunobtrusivevalidationattributes(         expressionhelper.getexpressiontext(expression),         metadata     ); 

then combine validationattributes dictionary other html attributes passed custom helper , pass along dropdownlistfor

the complete code i'm using (i have label in there too, can feel free de-couple):

public static ihtmlstring dropdownlistwithlabelfor<tmodel, tproperty>(this htmlhelper<tmodel> helper, expression<func<tmodel, tproperty>> expression, string label, ienumerable<selectlistitem> items, string blankoption, object htmlattributes = null) {     var l = new tagbuilder("label");     var br = new tagbuilder("br");      var metadata = modelmetadata.fromlambdaexpression(expression, helper.viewdata);     var mergedattributes = helper.getunobtrusivevalidationattributes(expressionhelper.getexpressiontext(expression), metadata);      if (htmlattributes != null)     {         foreach (propertydescriptor descriptor in typedescriptor.getproperties(htmlattributes))         {             object value = descriptor.getvalue(htmlattributes);             mergedattributes.add(descriptor.name, value);         }     }      l.innerhtml = label + br.tostring(tagrendermode.selfclosing) + helper.dropdownlistfor(expression, items, blankoption, mergedattributes);     return mvchtmlstring.create(l.tostring(tagrendermode.normal)); } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -