Instead of using Html.RenderPartial() put your partial view in Shared / EditorTemplates / and name its type name (in this case ZipCodeViewModel ). Then you can use Html.EditorFor() passing in the expression for the property. EditorFor () is the correct field prefix so that the mediator in MVC can correctly place your data.
So something like this
@model UserViewModel ..... @Html.EditorFor(m => m.ZipCode);
I will try to dig out some links and resources, but I want to get an answer for you.
EDIT : This blog post seems to cover the basics of the EditorFor () extension and EditorTempaltes: ASP.NET MVC Tips: Tempaltes Editor
In any case, when you use the extensions Html.TextBoxFor() or Html.DropDownFor() , what they do is to get the full name of the property from the expression you are passing into and set the attributes of the name and identifier of the HTML element for it. An example could be:
@Html.TextBoxFor(m => m.Person.FirstName)
will display
<input id="Person_FirstName" name="Person.FirstName" type="text" value="<what ever was in FirstName>" />
These input names and values ββare key / value pairs in the message data. The ASP.NET MVC middleware, by default, attempts to map these keys to models that include the actions of your controller. If he can find a match, he sets the value.
So your problem above is probably because the input names in your partial view are from ZipCodeViewModel down, but your action accepts UserViewModel . EditorFor() a name prefix for you, so it can basically display a partial view that will send data back that can be bound to you. Although you yourself could set the name attributes on the input elements (or flatten everything, as you said in the comments), there are these helpers to do this for you.
I hope this helps explain what is happening and a possible solution for you.
Sean copenhaver
source share