Required Field Validator not working for JQuery chosen drop down list

戏子无情 提交于 2019-12-05 07:20:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!