title: MVC 5 Edit with ViewModel by reusing EF Generated Code
body: <p>I have a Business <strong>Model</strong> and an <strong>EditBusinessViewModel</strong>.</p>
  
  <p>In MVC 4 I would use code something like this to edit a record:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(MainMenu mainmenu)
  {
      if (ModelState.IsValid)
      {
          db.MainMenus.Attach(mainmenu);
          db.ObjectStateManager.ChangeObjectState(mainmenu, EntityState.Modified);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
      return View(mainmenu);
  }
  </code></pre>
  
  <p>Now the auto generated code in MVC 5 looks like this, I've modified this Action to only include fields from my EditBusinessViewModel and named it <strong>Edit2</strong>:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  [ValidateInput(false)]
  public ActionResult Edit2([Bind(Include = "ID,BusinessName,BusinessDescription,BusinessAddress,BusinessPhoneOne,BusinessPhoneTwo,BusinessWeb,BusinessEmail,BusinessMelRef")] EditBusinessViewModel business)
  {
      if (ModelState.IsValid)
      {
          db.Entry(business).State = EntityState.Modified;
          db.SaveChanges();
          return Redirect("~/Home/Index/" + business.ID);
      }
      return View(business);
  }
  </code></pre>
  
  <p>I have the <strong>Get</strong> part working, my <strong>Model</strong> and <strong>View</strong> are working by returning:</p>
  
  <pre><code>return View(new EditBusinessViewModel(business));
  </code></pre>
  
  <p>But when I post back, I get an error on this line: </p>
  
  <pre><code>db.Entry(business).State = EntityState.Modified;
  </code></pre>
  
  <p><strong>The entity type EditBusinessViewModel is not part of the model for the current context.</strong> Which it is not and the reason for the ViewModel, I guess?</p>
  
  <p>What I would like to know is can I use this code or is there something else I should be doing?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I've been thinking about this too deeply and a ViewModel is just that, a <strong>ViewModel</strong> so  now I have:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  [ValidateInput(false)]
  public ActionResult Edit2([Bind(Include = "ID,BusinessDescription,BusinessAddress,BusinessPhoneOne,BusinessPhoneTwo,BusinessWeb,BusinessEmail,BusinessMelRef")] EditBusinessViewModel business)
  {
      if (ModelState.IsValid)
      {
          business.UserEmail = User.Identity.GetUserName();
  
          Business newbus = db.Businesses.Find(business.ID);
          {
              newbus.BusinessDescription = business.BusinessDescription;
              newbus.BusinessAddress = business.BusinessAddress;
          };
  
          db.Entry(newbus).State = EntityState.Modified;
          db.SaveChanges();
          return Redirect("~/Home/Index/" + business.ID);
      }
      return View(business);
  }
  </code></pre>
  
  <p>This way I post back the data I need from the view in the View Model, find the entity in the database by its matching ID and update it with the EF scaffold code.</p>
  
  <p>Is there a better way?</p>