Wednesday, 4 March 2015

Example of multiple select check box list in MVC


Press Me

Add 3 files in bundel
 "~/Scripts/core.js",
 "~/Scripts/widget.js",
 "~/Scripts/multiselect.js"
Add one css file in
jquery.multiselect.css


@model MvcApplication5.ViewModel.EmpVM

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EmpVM</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CID)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.CID, new SelectList(Model.COUNTRYD,"CID","CNAME"), "Select", new { style = "width:200px"  })
            @Html.ValidationMessageFor(model => model.CID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.SID)
        </div>
        <div class="editor-field">
            <div>
                @Html.ListBoxFor(model => model.SID, new MultiSelectList(Model.STATED, "SID", "SNAME"), new { size = 3, style = "width:200px", @id = "ddl",@name="ddl" })
            </div>
            @Html.ValidationMessageFor(model => model.SID)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
    $(function () {
        $('#ddl').multiselect();
        $('#CID').change(function(){
            $.getJSON('@Url.Action("Get")', { CID: $('#CID :selected').val() }, function (data)
            {
                $("#ddl").empty();
                $.each(data, function (i, j) {
                    $("#ddl").append($('<option>').text(j.SNAME).attr("value", j.SID));
                });
                $("#ddl").multiselect();
                $("#ddl").multiselect("refresh");
            });
        });
       
    });
    </script>
}


Some example of the event
---------------------------------------------------------------------------------------------------------------------
$('.ui-widget').multiselect("widget").find("input:checked").length

  $("#lstWorkgroup").multiselect({
        click: function (event, ui) {
            var count = $("#lstWorkgroup").multiselect("getChecked").map(function () {
                return this.value;
            }).get();
            if (count.length > 0)
            {

            }
        }
    });
$("#lstWorkgroup").multiselect({
        checkAll: function (event, ui) {
            $('#errormsgWorkgroup').empty();
        }
    });



No comments:

Post a Comment