public ActionResult Edit(ApplicationUserModel model) { model.Roles = db.Roles.Select(x => new ApplicationRoleModel { Id = x.Id, Name = x.Name }).ToList(); if (ModelState.IsValid) { var user = userService.FindById(model.Id); if (user == null) { this.NotifyError("User not found."); return(RedirectToAction("List")); } var oldRoles = userService.GetRoles(model.Id).ToArray(); var newRoles = model.SelectedRoleNames; user.IsActive = model.IsActive; user.UserName = model.UserName; user.Email = model.Email; user.EmailConfirmed = model.EmailConfirmed; user.Description = model.Description; user.PictureId = model.AvatarId; user.Name = model.Name; user.Surname = model.Surname; user.Location = model.Location; #region AddOrRemoveRoles var rolesToRemove = oldRoles.Except(newRoles); foreach (var role in rolesToRemove) { userService.RemoveFromRole(user.Id, role); } var rolesToAdd = newRoles.Except(oldRoles); foreach (var role in rolesToAdd) { userService.AddToRole(user.Id, role); } #endregion AddOrRemoveRoles var result = userService.Update(user); if (result.Succeeded) { this.NotifySuccess("Successfully saved."); } else { this.NotifyError("User can not saved!"); } return(RedirectToAction("Edit", new { id = model.Id })); } return(View(model)); }