title: How do I use an edit viewmodel in MVC?
body: <p>I have been struggling with this for some time. I have a model and an edit view model so I can allow the user to both see the image that was uploaded before and upload a replacement. Everything works fine until I get to the <code>db.Entry</code> portion. The error is: </p>
  
  <blockquote>
    <p>The entity type EditCardViewModel is not part of the model for the current context. </p>
  </blockquote>
  
  <p>If I try to add <code>EditCardViewModel</code> to the <code>DbContext</code>, it wants a key and a table, which isn't going to happen. The ViewModel is just a way to pass data. How do I tell it to use the Cards context when saving from this ViewModel? </p>
  
  <p>Controller Edit Get:</p>
  
  <pre><code>public ActionResult Edit(int id = 0)
  {
  
      Card card = db.Cards.Find(id);
      ViewData["Abilities"] = card.CardAbilities.Select(a =&gt; a.AbilityID);
  
      if (card == null)
      {
          return HttpNotFound();
      }
  
      var editview = new EditCardViewModel(card);
      {
      } 
      return View(editview);
  }
  </code></pre>
  
  <p>Controller Edit Post:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(EditCardViewModel card) 
  {
      if (ModelState.IsValid)
      {
          if(card.ImageUpload != null)
          {
              string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
              savedFileName = Path.Combine(savedFileName, Path.GetFileName(card.ImageUpload.FileName));
              card.ImageUpload.SaveAs(savedFileName);
              card.Cards.ImageUrl = "\\Images\\" + Path.GetFileName(card.ImageUpload.FileName);
          }
          db.Entry(card).State = EntityState.Modified; //ERROR - Entity Type is not part of context
          db.SaveChanges();
  </code></pre>
  
  <p>Edit ViewModel:</p>
  
  <pre><code>public class EditCardViewModel
  {
      public Card Cards { get; set; }
      public HttpPostedFileBase ImageUpload { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Abilities { get; set; }
      public int[] SelectedAbilities { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Rarities { get; set; }
      public int SelectedRarities { get; set; }
      public IEnumerable&lt;SelectListItem&gt; MainTypes { get; set; }
      public int SelectedMainTypes { get; set; }
      public IEnumerable&lt;SelectListItem&gt; SubTypes { get; set; }
      public int SelectedSubTypes { get; set; }
      public IEnumerable&lt;SelectList&gt; CardSets { get; set; }
      public int SelectedCardSets { get; set; }
      public Rarity Rarity { get; set; }
      public MainType MainType { get; set; }
      public SubType SubType { get; set; }
      public CardSet CardSet { get; set; }
  
      public EditCardViewModel() { } //NEEDED OR PARAMETERLESS CONSTRUCTOR ERROR
      public EditCardViewModel(Card card) //NEEDED OR CANNOT PASS CARD MODEL
      {
          Cards = card;
      }
  
  }
  </code></pre>