//Get public ActionResult EditRoles(string id) { if (string.IsNullOrWhiteSpace(id)) return new HttpStatusCodeResult(HttpStatusCode.OK); var user = db.Users.Find(id); AdminUserViewModel AdminModel = new AdminUserViewModel(); var selected = id.ListUserRoles(); AdminModel.Roles = new MultiSelectList(db.Roles, "Name", "Name", selected); AdminModel.User = user; return PartialView(AdminModel); }
// GET: UserRoles public ActionResult EditUser(string id) { var user = db.Users.Find(id); var selected = user.Roles.Select(r => r.RoleId); AdminUserViewModel adminModel = new AdminUserViewModel() { Roles = new MultiSelectList(db.Roles, "Id", "Name", selected), UserList = new SelectList(db.Users, "Id", "Name", selected), User = user }; return View(adminModel); }
public ActionResult EditUser(string[] selectedRoles, AdminUserViewModel model) { var rolesBeforeAdd = model.User.Id.ListUserRoles(); foreach(string role in rolesBeforeAdd) { if(!selectedRoles.Contains(role)) { model.User.Id.RemoveUserFromRole(role); } } foreach(string role in selectedRoles) { model.User.Id.AddUserToRole(role); } return RedirectToAction("Index"); }
public ActionResult EditUser(string id, AdminUserViewModel model) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var user = db.Users.Find(id); UserRolesHelper helper = new UserRolesHelper(); foreach (var role in db.Roles.ToList()) { //If selected but user doesn't have, add if (model.SelectedRoles.Contains(role.Id)) { helper.AddUserToRole(id, role.Name); } //If not selected but user *does* have, remove else if (!model.SelectedRoles.Contains(role.Id)) { helper.RemoveUserFromRole(id, role.Name); } } return RedirectToAction("Index", new { id }); }