public async Task <IActionResult> Edit(int id, EmployeeRoleSelections employeeRoleSelections)
        {
            if (!employeeRoleSelections.roleSelections.Any(mbox => mbox.Selected))
            {
                ModelState.AddModelError("roleSelections", "You must select one role!");
            }
            if (id != employeeRoleSelections.employee.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(employeeRoleSelections.employee);
                    _context.SaveChanges();

                    var rolesSelected = _context.EmployeeRole.Where(e => e.EmployeeId == employeeRoleSelections.employee.Id).ToList();
                    _context.RemoveRange(rolesSelected);

                    foreach (RoleSelection roleSelection in employeeRoleSelections.roleSelections)
                    {
                        if (roleSelection.Selected)
                        {
                            EmployeeRole employeeRole = new EmployeeRole
                            {
                                RoleId     = roleSelection.role.Id,
                                EmployeeId = employeeRoleSelections.employee.Id
                            };
                            _context.EmployeeRole.Add(employeeRole);
                        }
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(employeeRoleSelections.employee.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(employeeRoleSelections));
        }
        public async Task <IActionResult> Edit(int id, EmployeeRoleSelections employeeRoleSelections)
        {
            if (!employeeRoleSelections.roleSelections.Any(m => m.Selected))
            {
                ModelState.AddModelError("roleSelections", "You must select at least one role!");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var allEmployeeRoles = _context.EmployeeRole.Where(e => e.EmployeeId == id).ToList();
                    if (allEmployeeRoles.Count > 0)
                    {
                        _context.RemoveRange(allEmployeeRoles);
                    }

                    foreach (RoleSelection roleSelection in employeeRoleSelections.roleSelections)
                    {
                        if (roleSelection.Selected)
                        {
                            _context.EmployeeRole.Add(new EmployeeRole {
                                EmployeeId = id, RoleId = roleSelection.role.Id
                            });
                        }
                    }

                    _context.Update(employeeRoleSelections.employee);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(employeeRoleSelections));
        }