View:
The below view consists of the code for both edit and insert
The below view consists of the code for both edit and insert
<div class="field"> @Html.LabelFor(model => model.ProviderInsuranceID.Qualifiers) @if (Model.ProviderInsuranceID.Id > 0) {
@* Render this conduction during the Edit Mode *@ <select name="ProviderInsuranceID.Qualifiers" multiple="multiple" id="ProviderInsuranceID_Qualifiers"> @{ if (!string.IsNullOrWhiteSpace(Model.SelectedQualifiers)) { IEnumerable<string> selQul = Model.SelectedQualifiers.Split(',').Select(v => v.ToString()); foreach (var qualifiers in Model.QualifiersList) { if (selQul.Contains(qualifiers.Value)) { @:<option selected="selected" value="@qualifiers.Value">@qualifiers.Text</option> } else { @:<option value="@qualifiers.Value">@qualifiers.Text</option> } } } } </select> } else {
@* Render this conduction during the Insert Mode *@
@Html.DropDownListFor(model => model.ProviderInsuranceID.Qualifiers, Model.QualifiersList, new { @multiple = "multiple" }) } @Html.ValidationMessageFor(model => model.ProviderInsuranceID.Qualifiers)
@* needs to append the selected values to the hidden field in comma separated like (2,5,6)
this is because rendering the content out the form tag , when we click submit button we are
unable to get the selected value in the controller . so we are saving the selected value in
the hidden field
*@ @Html.HiddenFor(model => model.SelectedQualifiers) </div>
Script:
<script type="text/javascript">
$(function () {
var $hidcallback = $("#SelectedQualifiers");
$("#ProviderInsuranceID_Qualifiers").multiselect({
click: function (event, ui) {
$hidcallback.text(ui.value + ' ' + (ui.checked ? 'checked' : 'unchecked'));
var inputs = $hidcallback.val();
if (inputs == "") {
inputs += ui.value;
}
else {
inputs += "," + ui.value;
}
var selvalues = inputs.split(',');
selvalues = unique(selvalues);
if (ui.checked == false) {
selvalues = jQuery.grep(selvalues, function (value) {
return value != ui.value;
});
}
// adding selected items to hidden filed.
$hidcallback.val(selvalues);
}
});
});
// removes duplicate number from the array.
function unique(list) {
var result = [];
$.each(list, function (i, e) {
if ($.inArray(e, result) == -1) result.push(e);
});
return result;
}
</script>
Refference URL:
http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/