Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of using <code>Html.RenderPartial()</code> put your partial view in Shared/EditorTemplates/ and name it the name of the type (in this case <code>ZipCodeViewModel</code>). Then you can use <code>Html.EditorFor()</code> passing in the expression for the property. EditorFor() prefixes the fields correctly so that the model binder in MVC can put your data in place correctly.</p> <p>So something like this</p> <pre><code>@model UserViewModel ..... @Html.EditorFor(m =&gt; m.ZipCode); </code></pre> <p>I'll try to dig up some links and resources but wanted to get the answer out there for you.</p> <p><strong>EDIT</strong>: This blog post seems to go over the basics of EditorFor() extension and EditorTempaltes: <a href="http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx" rel="noreferrer">Quick Tips About ASP.NET MVC: Editor Tempaltes</a></p> <p>Anyway, when you use those <code>Html.TextBoxFor()</code> or <code>Html.DropDownFor()</code> extensions, what they do is get the full name of the property from the expression you pass in and set the HTML input element's name and id attributes to that. Example could be:</p> <pre><code>@Html.TextBoxFor(m =&gt; m.Person.FirstName) </code></pre> <p>would render</p> <pre><code>&lt;input id="Person_FirstName" name="Person.FirstName" type="text" value="&lt;what ever was in FirstName&gt;" /&gt; </code></pre> <p>Those input names and values are the key/value pairs in the post data. The default ASP.NET MVC model binder tries to match those keys up to the models your controller actions take in. If it can find a match then it sets the value.</p> <p>So your problem above is probably because the input names in your partial view are from the <code>ZipCodeViewModel</code> down but your action is taking in a <code>UserViewModel</code>. The <code>EditorFor()</code> prefixes the names for you so it can basically render a partial view that will post data back that can be bound for you. Although you could set the name attributes yourself on the input elements (or flatten everything as you suggested in the comments) has these helpers in there to do it for you.</p> <p>I hope that helps explain what is happening and a possible solution for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload