I have a page in MVC3 with a model of "pageModel".
In this page I have:
@{ Html.RenderPartial("_subPage", Model.subModel); } (Pagemodel.submodel)
In my controller I am doing:
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Results(pagemodel model, string frmAction)
{
}
The page loads fine the first time, but when I postback into the httpPost
action, model.submodel
is always null.
My question is, how do I return an updated model from the RenderPartial (if at all). I can get my model INTO the partial, but not back!
You can also perform the following.
Your partial view will remain as is, using the @model SubModel
The problem with partials is that they do not preserve the navigational context. This means that any input fields that you might have put inside this partial will have incorrect names and the default model binder will not be able to retrieve the values back when you POST. Your HTML will look like this:
whereas the correct is:
In order to achieve this correct markup I would recommend you using editor templates.
So you replace:
with:
and then you move your
_subPage.cshtml
partial into~/Views/Shared/EditorTemplates/SubModelType.cshtml
whereSubModelType
is the type of thesubModel
property:Now when you look at the generated HTML the corresponding input field names should be prefixed with
subModel
and inside the POST controller action themodel.subModel
property will this time be properly initialized and populated from the values that were entered by the user in the input fields.you'll need to change your partialview to accept the top level model, i.e:
which would then render your properties in the partialview with the correct property names i.e. :
<input type="text" name="subModel.MyProperty" value="somevalue" />
It would also mean that your returned model in the
HttpPost
action will have to correct navigational relationship intact.this is just one of those caveats related to viewmodels and hierarchies. Oh, btw, in mvc3, you don't need the verbose
[AcceptVerbs(HttpVerbs.Post)]
for posts. You can simply use[HttpPost]