I’ve followed Steve Sanderson’s “Editing a variable length list, ASP.NET MVC 2-style” guide and created an MVC view to edit a list of items. Note i’m using MVC 3 so i’m not sure if there is a better way to do this. http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
The problem i have is one of the fields on my list is a dropdown list. i’ve managed to get the dropdown populated on each row but its not loading the selected value when the page loads. It is however saving the selected value but every time i edit the page i need to re-set all the dropdowns.
Does anyone know how to set the selected dropdown value for each “row” on the partial view?
My edit view has
@foreach (var item in Model.Roles)
{
@Html.Partial("RoleRow-Edit", item)
}
My partial view has
@using (Html.BeginCollectionItem("Roles"))
{
@Html.EditorFor(model => model.TemplateID)
@Html.DropDownList("PartyRoleID", (SelectList)ViewBag.PartyRoles)
@Html.EditorFor(model => model.DisplayName)
}
On my controller i have
ViewBag.PartyRoles = new SelectList(db.PartyRoles.OrderBy(c => c.Role), "Role", "Role");
I found a workaround : you should create the SelectList in the partial view, and set its intial value to the bounded value, so partial view will look like this :