user
user

Reputation: 833

Multiple selction in Dropdownlist of asp.net MVC 3

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

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

Upvotes: 5

Views: 12122

Answers (3)

Stonck
Stonck

Reputation: 156

You can try maybe something like this ...

@Html.ListBoxFor(m=>m.Country, new MultiSelectList(CountryList as SelectList, "CountryID", "Select"))

Upvotes: 13

Manish
Manish

Reputation: 1796

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.

Upvotes: 0

TGlatzer
TGlatzer

Reputation: 6288

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

Upvotes: 8

Related Questions