title: .net mvc 3 create and edit
body: <p>I've been struggling with creating and editing a model that contains another model as part of its model.</p>
  
  <p>SubCategory contains Category as a variable because it needs to be able to reference back to it. </p>
  
  <p>Now I solved the create part by exposing the view to a list of Categories, via the ViewBag and binding it to a dropdown (model => model.Category.ID, new SelectBox .. etc):</p>
  
  <pre><code>    public ActionResult Create()
      {
          ViewBag.Categories = db.Category.ToList();
          return View();
      } 
  
      [HttpPost]
      public ActionResult Create(SubCategory subcategory)
      {
          subcategory.Category = db.Category.Single(x =&gt; x.ID == subcategory.Category.ID);
          if (ModelState.IsValid)
          {
              db.SubCategory.Add(subcategory);
              db.SaveChanges();
              return RedirectToAction("Index");  
          }
  
          return View(subcategory);
      }
  </code></pre>
  
  <p>As you can see, I had to do a quickfix above to get the subcategory to correctly bind to the category (In the DB Subcategory as a column with CategoryID).</p>
  
  <p>Now when i attempted the same for the Edit function it did not work. Below is my Edit code:</p>
  
  <pre><code>    public ActionResult Edit(int id)
      {
          ViewBag.Categories = db.Category.ToList();
          SubCategory subcategory = db.SubCategory.Find(id);
          return View(subcategory);
      }
  
      [HttpPost]
      public ActionResult Edit(SubCategory subcategory)
      {
          subcategory.Category = db.Category.Single(x =&gt; x.ID == subcategory.Category.ID);
          if (ModelState.IsValid)
          {
              UpdateModel(subcategory);
              db.Entry(subcategory).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(subcategory);
      }
  </code></pre>
  
  <p>As you can see, same method as the create, Using a break-point i see that subcategory.Category is correctly populated, but for some reason it does not save the state and after the page has reloaded the reference to CategoryID is unchanged.</p>
  
  <p>Any ideas or help will be much appriciated, thanks.</p>
  
  <p>UPDATE</p>
  
  <pre><code>   [HttpPost]
      public ActionResult Edit(SubCategory subcategory)
      {
          subcategory.Category = db.Category.Single(x =&gt; x.ID == subcategory.Category.ID);
          if (ModelState.IsValid)
          {
              db.SubCategory.Single(x =&gt; x.ID == subcategory.ID).Name = subcategory.Name;
              db.SubCategory.Single(x =&gt; x.ID == subcategory.ID).Category = db.Category.Single(x =&gt; x.ID == subcategory.Category.ID);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(subcategory);
      }
  </code></pre>
  
  <p>This works but makes my eyes bleed, so if you have a better suggestion :):</p>