Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You had the correct approach with EditorTemplates, but keep in mind you need to populate the <code>CityDropDown</code>. So, the view should be handed off something like:</p> <pre><code>Person model = new Person() { Address = new Address { CityDropDown = new SelectListItem[]{ new SelectListItem { Selected = true, Text = "Select one..." }, new SelectListItem { Text = "Anywhere", Value = "Anywhere" }, new SelectListItem { Text = "Somewhere", Value = "Somewhere" }, new SelectListItem { Text = "Nowhere", Value = "Nowhere" } } } }; </code></pre> <p>Which would then make this view only consist of:</p> <pre><code>@Html.EditorForModel() </code></pre> <p>And then your <code>EditorTemplates</code> would pick up from there:</p> <p><strong>~/Views/shared/EditorTemplates/Address.cshtml</strong> (Note: this is based on <em>type</em> not property name)</p> <pre><code>@model MvcApplication.Models.Address @Html.DropDownListFor(x =&gt; x.City, Model.CityDropDown) @Html.EditorFor(x =&gt; x.Zip) </code></pre> <p><strong>~/Views/Shared/EditorTemplates/Person.cshtml</strong></p> <pre><code>@model MvcApplication.Models.Person @using (Html.BeginForm()) { @Html.EditorFor(x =&gt; x.Name) @Html.EditorFor(x =&gt; x.Address) &lt;input type="submit" value="Save" /&gt; } </code></pre> <p>the three views then render something like:</p> <pre><code>&lt;form action="/" method="post"&gt; &lt;input class="text-box single-line" id="Name" name="Name" type="text" value="" /&gt; &lt;select id="Address_City" name="Address.City"&gt; &lt;option selected="selected"&gt;Select one...&lt;/option&gt; &lt;option value="Anywhere"&gt;Anywhere&lt;/option&gt; &lt;option value="Somewhere"&gt;Somewhere&lt;/option&gt; &lt;option value="Nowhere"&gt;Nowhere&lt;/option&gt; &lt;/select&gt; &lt;input class="text-box single-line" id="Address_Zip" name="Address.Zip" type="text" value="" /&gt; &lt;input type="submit" value="Save" /&gt; </code></pre> <p></p> <p>Example project can be found here: <a href="https://github.com/bchristie/StackOverflow-Examples/tree/master/questions-19247958" rel="nofollow">https://github.com/bchristie/StackOverflow-Examples/tree/master/questions-19247958</a></p>
    singulars
    1. This table or related slice is empty.
    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. 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