I would like make a pretty simple in my mind but really complicated when I tried to create. In a MVC ASP.NET environnement, I want to create one model but render it many times. It’s work but when I want get back datas I have nothing.
The model look like this:
public class HardwareModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
...
}
The wrapper look like:
public class WrapperModel
{
public List<HardwareModel> HardwareList { get; set; }
public WrapperModel()
{
HardwareList = new List<HardwareModel>();
}
}
The controller:
readonly Hardware _hardware = new Hardware();
[Authorize]
public ActionResult Index()
{
return View(_hardware.GetHardwareList(int.Parse(Session["idEmployee"].ToString())));
}
[HttpPost]
[Authorize]
public ActionResult Index(WrapperModel model)
{
return View(model);
}
And the view:
<% using (Html.BeginForm())
{%>
<% foreach (var hardware in Model.HardwareList)
{%>
<tr>
<td>
<div class="editor-label">
<%: Html.LabelFor(m => hardware.SelectedHardwareType) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(m => hardware.SelectedHardwareType, hardware.Hardwaretypes) %>
</div>
...
So the result is something like this:

The render is exactly what I want but the problem is that when I push a save button, the second part of the controller is used but the value of “WrapperModel model” is a empty List. In the Request value, I can see that everything is send to the controller but nothing match in the WrapperModel.
I don’t know what to do because the number of “HardwareModel” can be 0 or 99 so I can’t create HardwareModel1, HardwareModel2 … like I read on web.
thanks for helping me and sorry for the long post.
Use an EditorTemplate and you will be good.
Create a Folder called EditorTemplates annd Create aa view (the editortemplate) with the name
HardwareModel.cshtml
Now add the below code to the editor template (the new view). You may update the layout according to your requirement.
Now in your Main View, Use the
Html.EditorForHTML helper method to call this editor template.Assuming you have the GET Action sending a list of HardwareModels to the View like this
Now your HttpPost action method will get the Child Properties on form submit
Here you can download a working sample which i wrote to address your question.