问题
I am using JQuery Choosen for my DropDownList but the required validator for that dropdown is not working . Following is my code .Please guide where i am wrong.
Chosen plugin included in the form
$(".chosen-select").chosen();
DropDownList code
<li>
@Html.LabelFor(m => m.DepartmentId)
@Html.DropDownListFor(x => x.DepartmentId, (ViewBag.DepartmentsList) as IEnumerable<SelectListItem>, "-- Select an Option --",new { @class = "chosen-select", @style = "width:312px; height:31px;" })
@Html.ValidationMessageFor(m => m.DepartmentId)
</li>
And Model for the DropDownList
[Required(ErrorMessage = "*")]
[Display(Name = "Department Name")]
public int DepartmentId { get; set; }
回答1:
jQuery Chosen updates the html by making the original select hidden (style="Display:none"). By default jquery.validate (1.9.0) ignores hidden elements. You can override the default using
$.validator.setDefaults({
ignore: []
});
来源:https://stackoverflow.com/questions/31445293/required-field-validator-not-working-for-jquery-chosen-drop-down-list