开发者

Saving unknown number of identical Dropdowns on a Razor view

开发者 https://www.devze.com 2023-03-25 01:31 出处:网络
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 be

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 manage开发者_JAVA百科d 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 :

@{var selectList = new SelectList(db.PartyRoles.OrderBy(c => c.Role), 
                                 "Role", "Role",Model.PartyRoleID);} 
/*pass the db.PartyRoles.OrderBy(c => c.Role) in view bag inside controller,
it's cleaner*/

@using (Html.BeginCollectionItem("Roles"))
{
  @Html.EditorFor(model => model.TemplateID)
  @Html.DropDownList(model => model.PartyRoleID, selectList)
  @Html.EditorFor(model => model.DisplayName)
}


I can't see anywhere in your code where you would set the PartyRoleID value. I would recommend you to use view models and strongly typed views instead of ViewBag:

@Html.DropDownListFor(x => x.PartyRoleID, Model.PartyRoles)

Now all you have to do on your controller is to set the value of your view model:

var roles = db.PartyRoles.OrderBy(c => c.Role);
var model = new MyViewModel();
// preselect the value of the dropdown
// this value must correspond to one of the values inside the roles collection
model.PartyRoleID = "admin";
model.PartyRoles = new SelectList(roles, "Role", "Role");
return View(model);

Also I would recommend you using Editor Templates and replace the following foreach loop:

@foreach (var item in Model.Roles)
{
    @Html.Partial("RoleRow-Edit", item)
}

By the following:

@Html.EditorFor(x => x.Roles)

Now all that's left is to customize the corresponding editor template.

0

精彩评论

暂无评论...
验证码 换一张
取 消