Multiple selction in Dropdownlist of asp.net MVC 3
Asked Answered
O

3

5

Can we select multiple items from razor dropdownlist control. i.e for

@Html.DropDownListFor(m=>m.Country, CountryList as SelectList,"--Select--")

Ordinance answered 8/8, 2012 at 12:10 Comment(2)
I think you should use '@Html.ListBoxFor` for thisSalonika
Why not have a select list that looks like a drop down list? - ivaynberg.github.com/select2Raseda
D
13

You can try maybe something like this ...

@Html.ListBoxFor(m=>m.Country, new MultiSelectList(CountryList as SelectList, "CountryID", "Select"))
Dermatoglyphics answered 8/8, 2012 at 12:22 Comment(0)
L
8

You just have to add a new { "multiple" = "multiple" } as last Parameter of the function - this will render a multiselect.

Laundromat answered 8/8, 2012 at 12:18 Comment(3)
But how i will get the all selected values after submitingOrdinance
Thanks. I have resolved the problem of multiple selection but how i can retrieve all selected values in controllerOrdinance
This does create a multiple select box, but doesn't allow you to persist the selected options on POST. If the controller is returning a model error, then the user will lose their selected options.Trebuchet
C
0

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.

Colicweed answered 15/5, 2014 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.