title: MVC 4 - Entity Framework, ViewModel and Object Context
body: <p>I'm using Entity Framework with MVC 4 to develop a web application. I'm also using a ViewModel named VehicleTypeViewModel which is created like this :</p>
  
  <pre><code>public class VehicleTypeViewModel
  {
      public VehicleType VehicleType { get; set; }
  
      public ProductType ProductType { get; set; }
  
      [RegularExpression(@"^[a-zA-Zàéèêçñ\s][a-zA-Zàéèêçñ\s-]+$", ErrorMessage = "Invalid name !")]
      public string Name { get; set; }
  
      [Range(0, 300)]
      public int CO2 { get; set; }
  
      public List&lt;SelectListItem&gt; ProductCompanies { get; set; }
  
      public List&lt;SelectListItem&gt; MotorTypes { get; set; }
  }
  </code></pre>
  
  <p>In my Edit Action, everything's good but one thing : when I debug, arriving to the <code>db.Attach(...)</code> step, my app throws an exception which says : </p>
  
  <pre><code>The object cannot be attached because it is already in the object context. An object can only be reattached when it is in an unchanged state.
  </code></pre>
  
  <p>Here are my post action : </p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(VehicleTypeViewModel vtvm)
  {
      ViewBag.Id_VehicleMotorType = new SelectList(db.VehicleMotorTypes, "Id_VehicleMotorType", "Name", vtvm.VehicleType.Id_VehicleMotorType);
      ViewBag.Id_ProductCompany = new SelectList(db.ProductCompanies, "Id_ProductCompany", "Name", vtvm.ProductType.Id_ProductCompany);
  
      vtvm.ProductCompanies = db.ProductCompanies.ToList().Select(c =&gt; new SelectListItem { Text = c.Name, Value = c.Id_ProductCompany.ToString() }).ToList();
      vtvm.MotorTypes = db.VehicleMotorTypes.ToList().Select(v =&gt; new SelectListItem { Text = v.Name, Value = v.Id_VehicleMotorType.ToString() }).ToList();
  
      VehicleType vehicleType = db.VehicleTypes.Single(v =&gt; v.Id_VehicleType == vtvm.VehicleType.Id_VehicleType);
      ProductType productType = db.ProductTypes.Single(p =&gt; p.Id_ProductType == vtvm.ProductType.Id_ProductType);
      VehicleMotorType vehicleMotorType = null;
  
      ModelStateDictionary errors = Validator.isValid(vtvm.ProductType);
  
      if (ModelState.IsValid)
      {
          if (errors.Count &gt; 0)
          {
              ModelState.Merge(errors);
              return View(vtvm);
          }
  
          productType.Model = vtvm.ProductType.Model;
          productType.CatalogPrice = vtvm.ProductType.CatalogPrice;
          productType.Id_ProductCompany = vtvm.ProductType.Id_ProductCompany;
  
          if (!string.IsNullOrWhiteSpace(vtvm.Name) &amp;&amp; (vtvm.CO2 &gt; 0))
          {
              vehicleMotorType = new VehicleMotorType()
              {
                  CO2 = vtvm.CO2,
                  Name = vtvm.Name
              };
  
              vehicleType.CO2 = vtvm.VehicleType.CO2;
              vehicleType.VehicleMotorType = vehicleMotorType;
              vehicleType.Id_ProductType = vtvm.ProductType.Id_ProductType;
          }
          else
          {
              vehicleType.CO2 = vtvm.VehicleType.CO2;
              vehicleType.Id_ProductType = vtvm.ProductType.Id_ProductType;
              vehicleType.Id_VehicleMotorType = vtvm.VehicleType.Id_VehicleMotorType;
          }
  
          db.VehicleTypes.Attach(vehicleType);
          db.ProductTypes.Attach(productType);
  
          db.ObjectStateManager.ChangeObjectState(vehicleType, EntityState.Modified);
          db.ObjectStateManager.ChangeObjectState(productType, EntityState.Modified);
  
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      return View(vtvm);
  }
  </code></pre>
  
  <p>I have no idea why I'm dealing with this kind of error. Any idea to solve this please?</p>