Reputation: 19241
First of all, I have checked all the related questions but none of them solves my problem.
I am using ASP.NET MVC Html helper Dropdownlistfor with multiple property as follows.
@Html.DropDownListFor(m => m.SelectedPeople,
Model.People,
new { multiple = "multiple" })
/* Viewmodel for my view */
public class MyModel
{
public IEnumerable<SelectListItem> People { get; set; }
public IEnumerable<string> SelectedPeople { get; set; }
}
If i do not prepopulate the dropdownlist with some selected values. It works fine. Selected values are posted to action method without no problems.
However, if I want to prepopulate the dropdownlist, it is not working. I populate the dropdownlist as follows,
model.People = new[]
{
new SelectListItem { Value = "1", Text = "group 1" },
new SelectListItem { Value = "2", Text = "group 2" },
new SelectListItem { Value = "3", Text = "group 3" },
};
model.SelectedPeople = new[] { "2", "3" };
By not working, I mean SelectListItems 2 and 3 are not selected in DropDownList.
Upvotes: 0
Views: 678
Reputation: 22001
have you tried:
model.People = new[]
{
new SelectListItem { Value = "1", Text = "group 1" },
new SelectListItem { Value = "2", Text = "group 2", Selected = true },
new SelectListItem { Value = "3", Text = "group 3", Selected = true },
};
Upvotes: 1