title: Propogate Changes to Nested Entities in MVC 3
body: <p>I have two POCO classes a User and an Address.  Address is a complex object, and a User has one Address.  I want to create a single User view that allows the create/edit of an Address on the same form via MVC Scaffolding + Entity Framework + Repository Pattern.  The Create User View works correctly, but when I try to make changes to the Address in the User Edit View, the changes don't get propagated.  How do I force the changes to propogated down to the Address object from the User View?  Everything else is auto generated.</p>
  
  <h2>POCO Classes</h2>
  
  <pre><code>public class TestDB : DbContext
  {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
      }
  
      public DbSet&lt;Address&gt; Addresses { get; set; }
      public DbSet&lt;User&gt; Users { get; set; }
  }
  
  public class Address
  {
      public int ID { get; set; }
      public string Street1 { get; set; }
      public string Street2 { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string PostalCode { get; set; }
      public string Country { get; set; }
  }
  
  public class User
  {
      public int ID { get; set; }
      public string UserName { get; set; }
  
      /*[ForeignKey("Address")]
      public int AddressID { get; set; }*/
      public virtual Address Address { get; set; }
  }
  </code></pre>
  
  <h2>Edit User View</h2>
  
  <pre><code>&lt;% using (Html.BeginForm()) { %&gt;
  &lt;%: Html.ValidationSummary(true) %&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;User&lt;/legend&gt;
  
      &lt;%: Html.HiddenFor(model =&gt; model.ID) %&gt;
          &lt;%: Html.Partial("CreateOrEdit", Model) %&gt;
      &lt;legend&gt;Address&lt;/legend&gt;
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Address.Street1) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Address.Street1)%&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Address.Street1)%&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Address.Street2)%&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Address.Street2)%&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Address.Street2)%&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Address.City)%&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Address.City)%&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Address.City)%&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Address.State)%&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Address.State)%&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Address.State)%&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Address.PostalCode)%&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Address.PostalCode)%&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Address.PostalCode)%&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Address.Country)%&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Address.Country)%&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Address.Country)%&gt;
      &lt;/div&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  &lt;% } %&gt;
  
  &lt;div&gt;
      &lt;%: Html.ActionLink("Back to List", "Index") %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <h2>Editor Template for Address:</h2>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;AddressTest.Models.Address&gt;" %&gt;
  
  &lt;script src="&lt;%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %&gt;" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="&lt;%: Url.Content("~/Scripts/jquery.validate.min.js") %&gt;" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="&lt;%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %&gt;" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;% using (Html.BeginForm()) { %&gt;
  &lt;%: Html.ValidationSummary(true) %&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;Address&lt;/legend&gt;
  
      &lt;%: Html.HiddenFor(model =&gt; model.ID) %&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Street1) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Street1) %&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Street1) %&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Street2) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Street2) %&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Street2) %&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.City) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.City) %&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.City) %&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.State) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.State) %&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.State) %&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.PostalCode) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.PostalCode) %&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.PostalCode) %&gt;
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;%: Html.LabelFor(model =&gt; model.Country) %&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          &lt;%: Html.EditorFor(model =&gt; model.Country) %&gt;
          &lt;%: Html.ValidationMessageFor(model =&gt; model.Country) %&gt;
      &lt;/div&gt;
  
      &lt;p&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  &lt;% } %&gt;
  
  &lt;div&gt;
  &lt;%: Html.ActionLink("Back to List", "Index") %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <h2>Change to User Edit View:</h2>
  
  <pre><code>&lt;% using (Html.BeginForm()) { %&gt;
  &lt;%: Html.ValidationSummary(true) %&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;User&lt;/legend&gt;
  
      &lt;%: Html.HiddenFor(model =&gt; model.ID) %&gt;
          &lt;%: Html.Partial("CreateOrEdit", Model) %&gt;
      &lt;%: Html.Editor("Address", Model.Address) %&gt;
  
      &lt;p&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  &lt;% } %&gt;
  </code></pre>