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

asp.net-mvc-4, html, html.dropdownlistfor, jquery-chosen

Solution

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.

Problem

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

Original source