I have two classes and the first one contains a List. The VIEW receives a List as a model. When I post the view I'm able to retrieve in the controller each simple property. But the List<> is always null for the SubModels.
The two classes:
public class MyModel
{
public int ModelId {get;set;}
public string Description {get;set;}
public List<SubModel> SubModels {get;set;}
}
public class SubModel
{
public int MySubModelId {get;set;}
public string Description {get;set;}
}
In the view page the model received is
@model List<MyModel>
Following recommendations on internet, I created an editorfor editor to render my class in the view:
@model MyModel
<tr id="@Model.ModelId">
@Html.HiddenFor(m => m.ModelId)
@Html.HiddenFor(m => m.Description)
<td>@Model.Description</td>
</tr>
@for(int i = 0; i < Model.SubModels.Count; i++)
{
// How to render the class here to be able to be post in the controller?
// @Html.HiddenFor(m => m.SubModels[i]) will not work of course...
}
Unfortunately
HiddenFordoes not act recursively, but you can do it by hand:Just posting back
MySubModelIdfor each submodel would be enough to preventModel.SubModelsfrom being null.Usually I just postback the IDs, reloading everything else within the action method.