title: Related tables not saving in MVC application
body: <p>I have the following models - </p>
  
  <pre><code>public class RoleModel
  {
      public int Id { get; set; }
      public string RoleName { get; set; }
      public string Description { get; set; }
      public DateTime DateCreated { get; set; }
      public int CreatedBy { get; set; }
      public DateTime LastUpdated { get; set; }
      public int LastUpdateBy { get; set; }
  
      [NotMapped]
      public State State { get; set; }
  
      public virtual IEnumerable&lt;UserModel&gt; Users { get; set; }
      public virtual IEnumerable&lt;UserModel&gt; UsersNotInRole { get; set; }
      public virtual int[] SelectedUsers { get; set; }
  
      public virtual List&lt;RightModel&gt; Rights { get; set; }
      public virtual List&lt;RightModel&gt; SelectedRights { get; set; }
  
      public RoleModel()
      {
  
      }
  }
  
  public class RightModel
  {
      public string RightName { get; set; }
      public string Description { get; set; }
      public bool Assigned { get; set; }
  }
  </code></pre>
  
  <p>From this, each role has a collection of rights assigned to them. From the following view, I wish to allow users to select a check box against each right they want to assign to the selected role. The view is loading the data correctly and checking the correct boxes, but when I press 'Save', the Rights list is null. Any ideas how I can correct this so that it removes all rights from the selected role, then re-assigned the required rights.</p>
  
  <pre><code> @model Project.Core.Models.Roles.RoleModel
   @using (Html.BeginForm())
   {
        @Html.ValidationSummary(true)
        &lt;div class="tab-pane" id="tab_1_3"&gt;
                                      &lt;table class="table table-striped"&gt;
                                          &lt;thead&gt;
                                              &lt;tr&gt;
                                                  &lt;th&gt;Right Name&lt;/th&gt;
                                                  &lt;th&gt;Description&lt;/th&gt;
                                                  &lt;th&gt;&lt;/th&gt;
                                              &lt;/tr&gt;
                                          &lt;/thead&gt;
                                          &lt;tbody&gt;
                                              @foreach (var right in Model.Rights)
                                              {
                                                  &lt;tr&gt;
                                                      &lt;td&gt;@Html.DisplayFor(model =&gt; right.RightName)&lt;/td&gt;
                                                      &lt;td&gt;@Html.DisplayFor(model =&gt; right.Description)&lt;/td&gt;
                                                      &lt;td&gt;
                                                          &lt;div class="success-toggle-button"&gt;
                                                              @Html.CheckBoxFor(model =&gt; right.Assigned, new { @class = "toggle" })
                                                          &lt;/div&gt;
                                                      &lt;/td&gt;
                                                  &lt;/tr&gt;
                                              }
                                          &lt;/tbody&gt;
                                      &lt;/table&gt;
                                  &lt;/div&gt;
  </code></pre>
  
  <p>and my controller - </p>
  
  <pre><code>[Authorize]
      public ActionResult Details(int id = 0)
      {
          RoleModel role = _roleService.GetById(id);
  
          if (role == null)
          {
              return HttpNotFound();
          }
          return View(role);
      }
  
      [HttpPost]
      public ActionResult Details(RoleModel model)
      {
          if (ModelState.IsValid)
          {
              _roleService.Update(model);
  
              return RedirectToAction("Index");
          }
  
          return View(model);
      }
  </code></pre>
  
  <p>and my Update method in RoleService - </p>
  
  <pre><code>public void Update(RoleModel entity)
      {
          entity.LastUpdated = DateTime.Now;
          entity.LastUpdateBy = 1;
  
          Role r = _roleRepository.FindById(entity.Id);
  
          AutoMapper.Mapper.CreateMap&lt;RoleModel, Role&gt;();
  
          _roleRepository.Update(AutoMapper.Mapper.Map(entity, r));
      }
  </code></pre>
  
  <p>and my repository update method - </p>
  
  <pre><code> public void Update(Role role)
      {
          _context.ObjectStateManager.ChangeObjectState(role, EntityState.Modified);
  
          SaveChanges();
      }
  </code></pre>