I am not sure why the jquery validation fires when the property is nullable int?
and not marked with any data annotation attribute. Its asp.net MVC 4 application
public class MyViewModel{
public int? ResourceId { get; set; }
}
MyView.cshtml
@model MyViewModel
@Html.DropDownListFor(m => m.ResourceId,
((IEnumerable<SelectListItem>)ViewData["resourceLookup"]),"--Select--")
Generated HTML
<select data-val="true" data-val-number="The field ResourceId must be a number."
id="ResourceId" name="ResourceId">
<option value="">--Select--</option>
<option value="1">Type 1</option>
</select>
This select box is an optional one. User may ignore it. But when they ignore i am getting error message The field ResourceId must be a number.
In Global.asax.cs, I tried setting AddImplicitRequiredAttributeForValueTypes property to prevent adding the data-* attributes to unwanted fields. But as the name says it prevents only adding data-required-* attributes.
protected void Application_Start()
{
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes=false;
}
How can I prevent the data type checking attribute(data-val-number) here?
EDIT:
Tried setting DataType also no use, still having same issue. It looks like this will not be applied for <select>
and may be applied only for <input>
and set type="text"
[DataType(DataType.Text)]
public int? ResourceId { get; set; }
I had the same problem and just switched off client-side validation for my dropdown list. You can deactivate validation for this field by setting data-val = "false":
@Html.DropDownListFor(m => m.ResourceId,
((IEnumerable<SelectListItem>)ViewData["resourceLookup"]),"--Select--", new { @data_val = "false" })
This will not remove the data-val-number attribute but prevent the validation.
When you set the ViewData["resourceLookup"]
try adding -1
for the option Select
.
Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.