Here is my situation -
I have two nested view models:
<%=Html.EditorFor(x => x.DisplayEntitiesWithRadioboxesViewModel)%><br />
Which sits inside its parent (StructureViewModel), I can easily populate the nested ViewModels and pass it to the main view:
Inside the controller - an example
var moveDepartment = new StructureViewModel(); moveDepartment.DisplayEntitiesWithRadioboxesViewModel = fullDepartmentList.Select(x => new DisplayEntityViewModel { Id = x.Id, Path = x.Path, PathLevel = x.PathLevel, Description = x.Description, }); return View(moveDepartment);
EditorTemplete - Example
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Site.Areas.Administration.ViewModel.DisplayEntityViewModel>>" %> <table class="aligncenter"><% if (Model != null) { foreach (var entity in Model) {%> <tr class="tRow"> <td style="text-align:left; text-indent:<%=Html.Encode(entity.PathLevel)%>em"> <%=Html.Encode(entity.Description)%> <%=Html.RadioButton("radiobutton",entity.Id)%> </td> </tr><% } }%> </table> namespace Site.Areas.Administration.ViewModel { public class DisplayEntityViewModel { public int Id { get; set; } public string Path { get; set; } public string PathLevel { get; set; } public string Description { get; set; } } }
However, when I try to undo this information, nested ViewModels are null:
[HttpPost] public ActionResult Move(StructureViewModel StructureViewModel)
When I am above StructureViewModel , it contains only the dataset in the parent ViewModel. For example: a hidden value can be seen, but DisplayEntitiesWithRadioboxesViewModel = null.
The only way to find out how to access DisplayEntitiesWithRadioboxesViewModel is to use a FormCollection and iterate over the FormCollection and pull the information I need from the nested ViewModels.
However, this does not look like what I found in I, then I need to FormCollection DisplayEntitiesWithRadioboxesViewModel values from the FormCollection if, for example, an error occurs and the user should be sent back to the same view.
I tried searching on the Internet / books, but cannot find a solution.
Is there a better way?
Thanks in advance for your help.
And why did you use the editor for a simple dropdown menu that is easy to use with DropDownFor
Now it has been modified to use DropDownFor.
what is the key DisplayEntitiesWithRadioboxesViewModel value in FormCollection
{string[3]} [0] = "DisplayEntitiesWithRadioboxesViewModel.radiobutton" [1] = "Action" [2] = "OldParentId"
Clare :-)