title: Saving to m-2-m with checkboxes
body: <p>So i'm trying to use [Slauma's Answer][1]</p>
  
  <p>What i have done so far,</p>
  
  <p><strong>Model</strong> <em>ViewModelProspectUsers</em></p>
  
  <pre><code>    public int Id { get; set; }
      public string User { get; set; }
      public IEnumerable&lt;ViewModelUserProspectSelect&gt; Prospects { get; set; }
  </code></pre>
  
  <p><strong>Model</strong> <em>ViewModelUserProspectSelect</em></p>
  
  <pre><code>    public int ProspectID { get; set; }
      public string Name { get; set; }
      public bool IsSelected { get; set; }
  </code></pre>
  
  <p><strong>View</strong> <em>UserInProspect</em></p>
  
  <pre><code>@model OG.ModelView.ViewModelProspectUsers
  
  @using (Html.BeginForm())
  {
  
  &lt;div class="container"&gt;
      &lt;div class="contentContainer"&gt;
          @Html.HiddenFor(model =&gt; model.Id)
          Prospects for User &lt;h3&gt;@Html.DisplayTextFor(model =&gt; model.User)&lt;/h3&gt;
      &lt;/div&gt;
      &lt;div class="contentContainer2"&gt;
          &lt;h5&gt;Please Select Prospects you wish to assign to this User.&lt;/h5&gt;
          &lt;p&gt;&lt;/p&gt;
  
              @Html.EditorFor(model =&gt; model.Prospects)
  
      &lt;/div&gt;
      &lt;input type="submit" value="Save changes" /&gt;
      @Html.ActionLink("Cancel", "Index")
  &lt;/div&gt;
  }
  </code></pre>
  
  <p><strong>Editor View</strong> Located under ChangeUserInfo/EditorTemplates/*_ViewModelUserprospectSelect.cshtml*</p>
  
  <pre><code>@model OG.ModelView.ViewModelUserProspectSelect
  
  @Html.HiddenFor(model =&gt; model.ProspectID)
  @Html.HiddenFor(model =&gt; model.Name)
  test
  @Html.LabelFor(model =&gt; model.IsSelected, Model.Name)
  @Html.EditorFor(model =&gt; model.IsSelected)
  </code></pre>
  
  <p><strong>[Get]</strong> Method <em>UserInProspect</em> Action</p>
  
  <pre><code>public ActionResult UsersInProspect(int id = 0)
      {
          var data = db.UserProfiles
          .Where(s =&gt; s.UserID == id)
          .Select(s =&gt; new
          {
              ViewModel = new ViewModelProspectUsers
              {
                  Id = s.UserID,
                  User = s.UserName
              },
              prospects = s.Prospects.Select(c =&gt; c.ProspectID)
          })
          .SingleOrDefault();
  
          if (data == null)
              return HttpNotFound();
  
          // Load all companies from the DB
          data.ViewModel.Prospects = db.Prospect
              .Select(c =&gt; new ViewModelUserProspectSelect
              {
                  ProspectID = c.ProspectID,
                  Name = c.ProspectName
              })
              .ToList();
  
          // Set IsSelected flag: true (= checkbox checked) if the company
          // is already related with the subscription; false, if not
          foreach (var c in data.ViewModel.Prospects)
              c.IsSelected = data.prospects.Contains(c.ProspectID);
  
          return View(data.ViewModel);
  
      }
  </code></pre>
  
  <p><strong>[HttpPost]</strong> Method <em>UserInProspect</em> Action</p>
  
  <pre><code>    public ActionResult UsersInProspect(ViewModelProspectUsers viewModel)
      {
          if (ModelState.IsValid)
          {
              var subscription = db.UserProfiles.Include(s =&gt; s.Prospects)
                  .SingleOrDefault(s =&gt; s.UserID == viewModel.Id);
  
              if (subscription != null)
              {
                  // Update scalar properties like "Amount"
                  //subscription.Prospects = viewModel.Prospects;
                  //subscription. = subscription.
                  //List&lt;string&gt; myList = new List&lt;string&gt;();
                  //myList = viewModel.Prospects.Cast&lt;String&gt;().ToList();
  
                  //IEnumerable&lt;dbProspect&gt; Isubscription = subscription.Prospects;
                  ////or explicit:
                  //var iPersonList = (IEnumerable&lt;dbProspect&gt;)myList;
  
  
                  // or more generic for multiple scalar properties
                  // _context.Entry(subscription).CurrentValues.SetValues(viewModel);
                  // But this will work only if you use the same key property name
                  // in ViewModel and entity
  
                  foreach (var prospect in viewModel.Prospects)
                  {
                      if (prospect.IsSelected)
                      {
                          if (!subscription.Prospects.Any(
                              c =&gt; c.ProspectID == prospect.ProspectID))
                          {
                              // if company is selected but not yet
                              // related in DB, add relationship
                              var addedProspect = new dbProspect { ProspectID = prospect.ProspectID };
                              db.Prospect.Attach(addedProspect);
                              subscription.Prospects.Add(addedProspect);
                          }
                      }
                      else
                      {
                          var removedProspect = subscription.Prospects
                              .SingleOrDefault(c =&gt; c.ProspectID == prospect.ProspectID);
                          if (removedProspect != null)
                              // if company is not selected but currently
                              // related in DB, remove relationship
                              subscription.Prospects.Remove(removedProspect);
                      }
                  }
  
                  db.SaveChanges();
              }
  
              return RedirectToAction("Index");
          }
  
          return View(viewModel);
      }
  </code></pre>