Can we select multiple items from razor dropdownlist control. i.e for
@Html.DropDownListFor(m=>m.Country, CountryList as SelectList,"--Select--")
Can we select multiple items from razor dropdownlist control. i.e for
@Html.DropDownListFor(m=>m.Country, CountryList as SelectList,"--Select--")
You can try maybe something like this ...
@Html.ListBoxFor(m=>m.Country, new MultiSelectList(CountryList as SelectList, "CountryID", "Select"))
You just have to add a new { "multiple" = "multiple" }
as last Parameter of the function - this will render a multiselect.
Given a list of Items (in this example with fields Id and Name), you can start with a List of SelectListItem as follows:
List<SelectListItem> Choices = Items.Select(x => new SelectListItem { Value = Convert.ToString(x.Id).Trim(), Text = x.Name }).ToList();
@Html.ListBox("ListBoxIds", new MultiSelectList(Choices, "Value", "Text"))
In the controller, you will get ListBoxIds as a list of selected Ids.
© 2022 - 2024 — McMap. All rights reserved.