I am using chosen.js
for multiple selection, implemented with asp.net
mvc3
. Client side validation is not working this multi select control.
The way I implement is, using Html.Dropdownlist
helper and once the dom loaded from JavaScript $("#dropdown1").chosen()
.
It is replacing the Select with div's.
Below is the code
callback code
function (data) {
$("#divnewdata").html(data);
$("#ddlcode1").chosen({
disable_search_threshold: 10,
no_results_text: "Oops, nothing found!",
disable_search: false,
});
$("#ddlCode1_chosen").find('.chosen-choices').removeClass('form-control').addClass('form-control');
$("#ddlcode2").chosen({
disable_search_threshold: 10,
no_results_text: "Oops, nothing found!",
disable_search: false,
});
$("#ddlcode2_chosen").find('.chosen-choices').removeClass('form-control').addClass('form-control');
$.validator.setDefaults({
ignore: []
});
}
Loading java scripts from layout page
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script>
$('form').validate({
ignore: []
});
</script>
<script src="~/Scripts/Common/Common.js"></script>
</head>
partial view am trying to download
<script>
$(document).ready(function () {
$.validator.unobtrusive.parse("#formAddcodes");
$.validator.setDefaults({
ignore: []
});
});
</script>
<link href="~/Content/chosen.css" rel="stylesheet" />
<script src="~/Scripts/chosen.jquery.min.js"></script>
<script src="~/Scripts/Codes/Codes.js"></script>
@using (Html.BeginForm("AddCodes", "Codes", FormMethod.Post, new { id = "formAddcodes" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.code1, htmlAttributes: new { @class = "col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.code1, Model.Code1List as SelectList, new { @class = "form-control", id = "ddlcode1", multiple = "multiple" })
@Html.ValidationMessageFor(model => model.code1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.code2, htmlAttributes: new { @class = "col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.code2, Model.code2List as SelectList, new { @class = "form-control", id = "ddlcode2", multiple = "multiple" })
@Html.ValidationMessageFor(model => model.code2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Add" class="btn btn-primary" />
</div>
</div>
</div>
}
chosen
updates the html by making the original <select>
element hidden and hidden elements are ignored by default. You can modify this behavior using either the following scripts
$.validator.setDefaults({
ignore: []
});
or
$('form').validate({
ignore: []
});