Saving unknown number of identical Dropdowns on a Razor view?

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.

Up vote 0 down vote favorite share g+ share fb share tw.

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.

blog.stevensanderson.com/2010/01/28/edit... 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"); c# asp. Net-mvc asp.

Net-mvc-3 razor link|improve this question edited Aug 4 '11 at 13:52Darin Dimitrov304k21345557 asked Aug 4 '11 at 13:40Jammy1036 100% accept rate.

I have the same problem and can't solve it – tahir Aug 22 '11 at 12:46.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions