title: ASP.NET MVC Viewmodel Edit returns empty fields (item is not identified by id)
body: <p>I previously reveived some help <a href="https://stackoverflow.com/questions/47377582/asp-net-mvc-viewmodel-returns-nothing">here</a> but some problems with the "Edit" arouse now. Currently stuck at HttpGet, HttpPost is yet to come.</p>
  
  <p>In short, whenever I click on an existing item to edit, instead of the expected values, it returns an empty form, <a href="https://imgur.com/5OYz4FZ" rel="nofollow noreferrer">as seen here</a>. (You might think that the reason for this is because I use the same exact view for Create and Edit but unfortunately this isn't, even when I used to separate ones, had the same result.)</p>
  
  <p>Town.cs</p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace City.Models
  {
      public class Town
      {
          public Town()
          {
              Streets = new List&lt;Street&gt;();
          }
          public int TownId { get; set; }
          public string TownName { get; set; }
          public virtual ICollection&lt;Street&gt; Streets { get; set; }
      }
  }
  </code></pre>
  
  <p>Street.cs</p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace City.Models
  {
      public class Street
      {
          public Street()
          {
              Houses = new List&lt;House&gt;();
          }
          public int StreetId { get; set; }
          public string StreetName { get; set; }
          public virtual ICollection&lt;House&gt; Houses { get; set; }
      }
  }
  </code></pre>
  
  <p>House.cs</p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace City.Models
  {
      public class House
      {
          public House()
          {
              Floors = new List&lt;Floor&gt;();
          }
          public int HouseId { get; set; }
          public string HouseName { get; set; }
          public ICollection&lt;Floor&gt; Floors { get; set; }
      }
  }
  </code></pre>
  
  <p>Floor.cs</p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace City.Models
  {
      public class Floor
      {
          public Floor()
          {
              FireExtinguishers = new List&lt;FireExtinguisher&gt;();
          }
          public int FloorId { get; set; }
          public int FloorNumber { get; set; }
          public virtual ICollection&lt;FireExtinguisher&gt; FireExtinguishers { get; set; }
      }
  }
  </code></pre>
  
  <p>FireExtinguisher.cs</p>
  
  <pre><code>using System.ComponentModel;
  
  namespace City.Models
  {
      public class FireExtinguisher
      {
          public int FireExtinguisherId { get; set; }
          [DisplayName("Fire Extinguisher")]
          public string FireExtinguisherName { get; set; }
          public int FloorId { get; set; }
          public int HouseId { get; set; }
          public int StreetId { get; set; }
          public int TownId { get; set; }
          public Floor Floor { get; set; }
          public House House { get; set; }
          public Street Street { get; set; }
          public Town Town { get; set; }
      }
  }
  </code></pre>
  
  <p>MyViewModel.cs</p>
  
  <pre><code>using System.Collections.Generic;
  
  namespace City.Models
  {
      public class MyViewModel
      {
          public IEnumerable&lt;Town&gt; TownId { get; set; }
          public IEnumerable&lt;Street&gt; StreetId { get; set; }
          public IEnumerable&lt;House&gt; HouseId { get; set; }
          public IEnumerable&lt;Floor&gt; FloorId { get; set; }
          public FireExtinguisher FireExtinguisher { get; set; }
          public string FireExtinguisherName { get; set; }
      }
  }
  </code></pre>
  
  <p>Create/Edit view</p>
  
  <pre><code>@model City.Models.MyViewModel
  
  &lt;h2&gt;Add new or edit existing FE&lt;/h2&gt;
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
          &lt;div class="col-md-12"&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.TownId, "Town", new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.DropDownListFor(model =&gt; model.TownId, new SelectList(Model.TownId, "TownId", "TownName"), "Choose Town", new { @class = "form-control" })
                      @Html.ValidationMessageFor(model =&gt; model.TownId, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.StreetId, "Street", new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.DropDownListFor(model =&gt; model.StreetId, new SelectList(Model.StreetId, "StreetId", "StreetName"), "Choose Street", new { @class = "form-control" })
                      @Html.ValidationMessageFor(model =&gt; model.StreetId, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.HouseId, "House", new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.DropDownListFor(model =&gt; model.HouseId, new SelectList(Model.HouseId, "HouseId", "HouseName"), "Choose House", new { @class = "form-control" })
                      @Html.ValidationMessageFor(model =&gt; model.HouseId, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.FloorId, "Floor", new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.DropDownListFor(model =&gt; model.FloorId, new SelectList(Model.FloorId, "FloorId", "FloorNumber"), "Choose Floor", new { @class = "form-control" })
                      @Html.ValidationMessageFor(model =&gt; model.FloorId, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  @Html.LabelFor(model =&gt; model.FireExtinguisherName, "Fire Extinguisher", new { @class = "control-label col-md-2" })
                  &lt;div class="col-md-10"&gt;
                      @Html.TextBoxFor(model =&gt; model.FireExtinguisherName, new { @class = "form-control", Value = "" })
                      @Html.ValidationMessageFor(model =&gt; model.FireExtinguisherName, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
  
  
          &lt;div&gt;
              &lt;div&gt;
                  &lt;input type="submit" value="Do it" 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;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p>Homecontroller.cs</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Net;
  using System.Web.Mvc;
  using City.Models;
  
  namespace City.Controllers
  {
      public class HomeController : Controller
      {
          private ApplicationDbContext db;
          private MyViewModel viewModel;
  
          public HomeController()
          {
              db = new ApplicationDbContext();
  
              var town = db.Towns.ToList();
              var street = db.Streets.ToList();
              var house = db.Houses.ToList();
              var floor = db.Floors.ToList();
  
              viewModel = new MyViewModel()
              {
                  TownId = town,
                  StreetId = street,
                  HouseId = house,
                  FloorId = floor
              };
          }
          public ActionResult Index()
          {
              return View(db.FireExtinguishers.ToList());
          }
  
          [HttpGet]
          public ActionResult Create()
          {
              return View(viewModel);
          }
  
          [HttpPost]
          public ActionResult Create(FireExtinguisher fe)
          {
              if (ModelState.IsValid)
              {
                  db.FireExtinguishers.Add(fe);
                  db.SaveChanges();
  
                  return RedirectToAction("Index");
              }
              return View(viewModel);
          }
  
          [HttpGet]
          public ActionResult Edit(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              var fe = db.FireExtinguishers.Find(id);
  
              var town = db.Towns.ToList();
              var street = db.Streets.ToList();
              var house = db.Houses.ToList();
              var floor = db.Floors.ToList();
  
              viewModel = new MyViewModel()
              {
                  FireExtinguisher = fe,
  
                  TownId = town,
                  StreetId = street,
                  HouseId = house,
                  FloorId = floor
              };
              return View("Create", viewModel);
          }
  
          [HttpPost]
          public ActionResult Edit()
          {
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>
  
  <p>Any help would be appreciated, thank you</p>