title: Update relational DB data based on MVC actionresult (Entity Framework)
body: <p>I'm trying to update a table within my DB to change a particular relationship based on a user selecting a card, and then click a button to send that card to that deck, see below: </p>
  
  <p>My Models: </p>
  
  <pre><code>public class Card
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int? Atk { get; set; }
      public int? Def { get; set; }
      public string Desc {get; set;}
      public int? Level { get; set; }
      public string Type { get; set; }
      public string Attribute { get; set; }
      [DisplayName("Image")]
      public virtual List&lt;Image&gt; Card_Images { get; set; }
  
      public virtual List&lt;Deck&gt; Deck { get; set; }
  
  
  }
  
  
  
      public class Deck
      {
          public int id { get; set; }
  
          public string Name { get; set; }
  
          public string Notes { get; set; }
          [DisplayName("Card")]
          public virtual List&lt;Card&gt; Card { get; set; }
      }
  </code></pre>
  
  <p>My Controller action: </p>
  
  <pre><code>    public ActionResult AddToDeck(int id)
      {
          var cardInDb = _context.Cards.Single(c =&gt; c.Id == id);
  
          var deckInDb = _context.Decks.Single(d =&gt; d.id == id);
  
          foreach (var c in cardInDb.Deck)
          {
              cardInDb.Id = deckInDb.id;
          }
  
  
          _context.SaveChanges();
  
          return View("Index");
      }
  </code></pre>
  
  <p>My View: </p>
  
  <pre><code>@using YGOBuilder.Controllers
  @model YGOBuilder.Models.Card
  
  &lt;div&gt;
      &lt;h4&gt;Card&lt;/h4&gt;
      &lt;hr /&gt;
      &lt;dl class="dl-horizontal"&gt;
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Name)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Name)
          &lt;/dd&gt;
  
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Atk)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Atk)
          &lt;/dd&gt;
  
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Def)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Def)
          &lt;/dd&gt;
  
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Desc)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Desc)
          &lt;/dd&gt;
  
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Level)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Level)
          &lt;/dd&gt;
  
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Type)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Type)
          &lt;/dd&gt;
  
          &lt;dt&gt;
              @Html.DisplayNameFor(model =&gt; model.Attribute)
          &lt;/dt&gt;
  
          &lt;dd&gt;
              @Html.DisplayFor(model =&gt; model.Attribute)
          &lt;/dd&gt;
  
          @foreach (var item in Model.Card_Images)
          {
  
              &lt;td&gt;
                  &lt;img src=@item.image_url height="300" width="200" style="margin: 2px"&gt;
              &lt;/td&gt;
  
          }
  
  
      &lt;/dl&gt;
  &lt;/div&gt;
  &lt;p&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/p&gt;
  </code></pre>
  
  <p>The DB Table I'm trying to manipulate looks as follows there is no model attached to this, this was created by Entity Framework for a many to many relationship: </p>
  
  <pre><code>DeckCards
      (key sign) Deck_id
      (key sign) Card_id
  </code></pre>
  
  <p>I want to somehow display a list of all current decks in the view above, would I need to create a viewModel for this? </p>
  
  <p>Also, what is the best approach for when the user is viewing a card with an id of 1, they then want to update the DeckCards table to pair that card with a deck of id 3, e.g </p>
  
  <pre><code> Deck_Id     Card_Id
      3            1
  </code></pre>