必需的字段验证器不适用于JQuery选择的下拉列表

时间:2015-07-16 04:19:01

标签: javascript jquery asp.net asp.net-mvc-4 jquery-chosen

我正在使用JQuery Choosen作为我的DropDownList,但该下拉列表所需的验证器无效。以下是我的代码。请指导我错的地方。

选择表格中包含的插件

 $(".chosen-select").chosen();

DropDownList代码

<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>

DropDownList的模型

[Required(ErrorMessage = "*")]
[Display(Name = "Department Name")]
public int DepartmentId { get; set; }

1 个答案:

答案 0 :(得分:3)

jQuery Chosen通过使原始选择隐藏来更新html(style =“Display:none”)。默认情况下,jquery.validate(1.9.0)忽略隐藏的元素。您可以使用

覆盖默认值
$.validator.setDefaults({ 
  ignore: []
});
相关问题