public ActionResult AssignRolesToUser(string returnUrl, string id = "") { //userName = "******"; ViewBag.ReturnUrl = returnUrl; ViewBag.PageTitle = "USER MANAGEMENT"; Dictionary <string, string> PanelTitles = new Dictionary <string, string>(); PanelTitles["PanelTitle1"] = "ROLES ASSIGNED TO"; ViewBag.PanelTitles = PanelTitles; if (TempData["SuccessMessage"] != null) { ViewBag.SuccessMessage = TempData["SuccessMessage"]; } if (TempData["ErrorMessage"] != null) { ViewBag.ErrorMessage = TempData["ErrorMessage"]; } if (TempData["WarningMessage"] != null) { ViewBag.WarningMessage = TempData["WarningMessage"]; } List <SelectListItem> allRoles = (new ViewHelper()).AllRolesToSelectListItem(); List <string> existingRoles = im.GetUserRolesByUserName(id); foreach (var role in allRoles) { if (existingRoles != null) { if (existingRoles.Contains(role.Text)) { role.Selected = true; } } } AssignRolesToUserViewModel model = new AssignRolesToUserViewModel() { UserName = id, Roles = allRoles }; return(View(model)); }
public ActionResult AssignRolesToUser(AssignRolesToUserViewModel model, string returnUrl) { ViewBag.ReturnUrl = returnUrl; if (!ModelState.IsValid) { TempData["ErrorMessage"] = Messages.CheckInput; return(RedirectToAction("AssignRolesToUser")); } IdentityManager im = new IdentityManager(); List <string> existingRoles = im.GetUserRolesByUserName(model.UserName); foreach (var role in model.Roles) { if (role != null) { if (role.Selected) { if (!existingRoles.Contains(role.Text)) { im.AddUserToRole(model.UserName, role.Text); } } else { if (existingRoles.Contains(role.Text)) { im.RemoveRoleByUserName(model.UserName, role.Text); } } } } TempData["SuccessMessage"] = Messages.UpdateSuccessfull; return(RedirectToAction("AssignRolesToUser", new { userName = model.UserName })); }