title: ASP .NET MVC 5 - Customer-Address One-To-One relationship
body: <p>I've looked into forums here and actually found some similar questions, but not the same ones. Similar solutions did not give me the right answer.</p>
  
  <p>I'm working with ASP .NET MVC 5 using Entity Framework and Code First approach.</p>
  
  <p>I would like to model Customer -> Address One-To-One relationship. What I've modeled is:</p>
  
  <p><strong>Customer class</strong></p>
  
  <pre><code>public class Customer
  {
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      [DisplayName("Middle Name")]
      public string MiddleName { get; set; }
  
      [DisplayName("Last Name")]
      public string LastName { get; set; }
  
  
      public int AddressId { get; set; }
  
      public virtual Address Address { get; set; }
  }
  </code></pre>
  
  <p><strong>Address class</strong></p>
  
  <pre><code>public class Address
  {
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      public string Country { get; set; }
  
      public string City { get; set; }
  
      public string PostalCode { get; set; }
  
      public string Street { get; set; }
  
      public string HouseNumber { get; set; }
  
  }
  </code></pre>
  
  <p>Having this relation modeled scaffolding mechanism gives the following (examples for Create methods):</p>
  
  <p><strong>Customer controller Create method</strong></p>
  
  <pre><code> // GET: Customers/Create
      public ActionResult Create()
      {
          ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Name");
          return View();
      }
  
      // POST: Customers/Create
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = "Id,Name,MiddleName,LastName,AddressId")] Customer customer)
      {
          if (ModelState.IsValid)
          {
              db.Customers.Add(customer);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Name", customer.AddressId);
          return View(customer);
      }
  </code></pre>
  
  <p><strong>Customer view Create file</strong></p>
  
  <pre><code>@model Models.Customer
  
  @{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Customer&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Name, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Name, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.MiddleName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.LastName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.AddressId, "AddressId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("AddressId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.AddressId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>The result I have now</strong>: a Create view with fields for all Customer's data and a DropDown list with already existing Addresses in the database to choose for newly created Customer.</p>
  
  <p><strong>Expected result</strong>: a Create view with fields for Customer's data and all Address's fields for the user to fill. When saving the Customer, new Address entity should be saved and assigned for newly created Customer.</p>
  
  <p>How is the best to achieve that? I know I can access my Customer's Address's fields in the view by simply doing model.Address.Name for instance, but how can I post it then to the controller and save into database?</p>
  
  <p>Thank you in advance!</p>