I’m using client-side, unobtrusive validation in MVC 3. I have a field called MinPrice only decorated with DisplayName and Range attributes. But it is failing client-side validation saying “The MinPrice field is required”. I have no idea why and I definitely don’t have a required attribute anywhere applied to it.
[DisplayName("Asking Price")]
[Range(0, 99999999, ErrorMessage="Invalid number")]
public int MinPrice { get; set; }
What’s causing it?
(Note: I can see the data-val-required=”The Minprice field is required” attribute rendered in the html source, so something to do with the new unobtrusive routines is putting it there). I don’t seem to have this problem with other fields..
FYI, This has nothing to do with Linq to SQL really.
An implicit
[Required]attribute is applied to MinPrice as it is anintwhich is, by definition, not-nullable (as opposed toint?which is nullable).This behaviour is implemented by
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypeswhich defaults to true.
You can disable this behaviour by setting it to false in your Global.asax