Partial problems are that they do not preserve the navigation context. This means that any input fields that you could place inside this partial will have incorrect names, and the default mediator will not be able to return values โโduring POST. Your HTML will look like this:
<input type="text" name="Prop1" value="property 1 value" /> <input type="text" name="Prop2" value="property 2 value" />
whereas correct:
<input type="text" name="subModel.Prop1" value="property 1 value" /> <input type="text" name="subModel.Prop2" value="property 2 value" />
To achieve this correct markup, I would recommend that you use editor templates.
So you are replacing:
@{ Html.RenderPartial("_subPage", Model.subModel); }
from:
@Html.EditorFor(x => x.subModel)
and then you move the partial _subPage.cshtml to ~/Views/Shared/EditorTemplates/SubModelType.cshtml , where SubModelType is the type of the subModel property:
@model SubModelType @Html.EditorFor(x => x.Prop1) @Html.EditorFor(x => x.Prop2)
Now, when you look at the generated HTML, the corresponding input field names must be prefixed with subModel and inside the POST controller action the model.subModel property this time will be correctly initialized and filled out from the values โโentered by the user in the input fields.
source share