使用 Chosen jquery 后,不会收到 @Html.DropDownListFor() 的验证消息 [英] Not getting validation message for @Html.DropDownListFor() once Chosen jquery is used
问题描述
代码如下:
如果我没有在组合框中选择任何值并按提交,则不会询问验证消息.
Code is given below:
If i do not select any value in the combobox and press submit, no validation message is asked.
<tr>
<td>Department </td>
<td> : </td>
<td class="@*@Model.NoEdit*@">
@Html.DropDownListFor(m => m.DepartmentId, new SelectList(Model.Departments, "SelectedDepartmentId", "DepartmentCode"), "-- Select Department--", new {@class = "chosen-select", id = "cboDeptartment" })
@Html.ValidationMessageFor(model => model.DepartmentId)
</td>
推荐答案
重申我的简短评论作为答案.:-)
Restating my brief comment as an answer. :-)
为了获取验证消息,我使用了 Joseph 提到的相同方法,但我对那些与 Chosen 一起使用的选择使用了 HTML 类.例如:
To get the validation messages, I use the same approach Joseph mentions, though I use an HTML class for those selects that I am using with Chosen. For example:
validator.settings.ignore = ":hidden:not(.select-chosen)";
要在选择值时清除验证消息,您需要为更改"事件附加一个事件侦听器,并明确强制重新验证选择.例如,假设您有一个引用选择框的变量 select
:
To clear the validation message when a value is selected, you need to attach an event listener for "change" events and explicitly force revalidation of the select. So for example, assuming you have a variable select
that references the select box:
select.on("change", function(evt, params) {
$(evt.target).valid();
});
Chosen 的值发生变化时会触发这些更改"事件.
Chosen triggers those "change" events whenever it's value changes.
这篇关于使用 Chosen jquery 后,不会收到 @Html.DropDownListFor() 的验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!