Not getting validation message for @Html.DropDownListFor() once Chosen jquery is used

巧了我就是萌 提交于 2019-11-27 08:09:14

问题


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>

回答1:


Restating my brief comment as an answer. :-)

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)";

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 triggers those "change" events whenever it's value changes.




回答2:


Solved after a lot of research...

$(document).ready(function () {
    var validator = $("#Your_form_id").data('validator');
    validator.settings.ignore = ":hidden:not(select)";
});




回答3:


So aligning the above answers properly

 $(function () {
        var validator = $("#Your_form_id").data('validator');
        validator.settings.ignore = ":hidden:not(.chosen-select)";
        $(".chosen-select").chosen();
        $("#Your_dropdown_id").change(function (evt, params) {
            $(evt.target).valid();
        })

    });


来源:https://stackoverflow.com/questions/20950610/not-getting-validation-message-for-html-dropdownlistfor-once-chosen-jquery-is

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