title: Foreign Key property won't set correctly
body: <p>I have made an earlier post with similar code, and have updated the models to use a helper class instead of a ViewBag. It seemed to work like a charm until the database was updated.</p>
  
  <p>These are my model classes.</p>
  
  <pre><code>[Table("Store")]
      public class Store
      {
          [Key]
          public int StoreId { get; set; }
  
          public string Name { get; set; }
  
          public string Address { get; set; }
      }
  </code></pre>
  
  <pre><code>    [Table("Purchase")]
      public class Purchase
      {
          [Key]
          public int Id { get; set; }
  
          public int StoreId { get; set; }
  
          [ForeignKey("StoreId")]
          public Store Store { get; set; }
      }
  
      public class PurchaseDBContext : DbContext
      {
          public DbSet&lt;Purchase&gt; Purchases { get; set; }
          public DbSet&lt;Store&gt; Stores { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
  
          }
      }
  </code></pre>
  
  <pre><code>public class PurchaseCreateHelper
      {
          public Purchase Purchase { get; set; }
          public List&lt;Store&gt; Stores { get; set; }
      }
  }
  </code></pre>
  
  <p>My relevant part of the View for this question:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.Purchase.StoreId, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.DropDownListFor(model =&gt; model.Purchase.StoreId, new SelectList(Model.Stores.Select( x =&gt; new { StoreId = x.StoreId, DisplayName = x.Name.ToString() + " - " + x.Address.ToString()}), "StoreId", "DisplayName"), new { @class = "form-control" })
              @Html.ValidationMessageFor(model =&gt; model.Purchase.StoreId, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>And the relevant part of my controller: </p>
  
  <pre><code>        // GET: Purchases/Create
          public ActionResult Create()
          {
              var purchaseHelper = new PurchaseCreateHelper()
              {
                  Stores = db.Stores.ToList(),
                  Purchase = new Purchase()
              };
  
              return View(purchaseHelper);
          }
  
          // POST: Purchases/Create
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create(Purchase purchase)
          {
              if (ModelState.IsValid)
              {
                  db.Purchases.Add(purchase);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              return View(purchase);
          }
  </code></pre>
  
  <p>When my code comes to db.Purchases.Add(purchase) the Store property of the purchase object is set to null. The StoreId however seems to be perfectly fine. I don't get why the Store object won't change with the StoreId property as I have annotated it with [ForeignKey("StoreId")]. Where did I mess up the connection between StoreId and Store?</p>
  
  <p>UPDATE #2</p>
  
  <pre><code>        // GET: Purchases
          public ActionResult Index()
          {
              return View(db.Purchases.ToList());
          }
  </code></pre>
  
  <pre><code>@model IEnumerable&lt;BookKeeper.Models.Purchase&gt;
  
  @{
      ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  
  &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Store.Name)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Type)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Price)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Date)
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
  &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; (item.Store.Name))
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Type)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Price)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Date)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
          @Html.ActionLink("Details", "Details", new { id = item.Id }) |
          @Html.ActionLink("Delete", "Delete", new { id = item.Id })
      &lt;/td&gt;
  &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>
  
  <p>UPDATE #3
  <a href="https://i.stack.imgur.com/2sE91.png" rel="nofollow noreferrer">enter image description here</a></p>