public async Task <IActionResult> EditRole(AdminUserEditRole vm) { if (ModelState.IsValid) { var user = _appContext.Users.Single(x => x.UserName == vm.User.UserName); // Get roles var roles = await _userManager.GetRolesAsync(user); // Remove roles foreach (var item in roles) { await _userManager.RemoveFromRoleAsync(user, item); } var role = _appContext.Roles.Find(vm.SelectedRole); // Set new role await _userManager.AddToRoleAsync(user, role.Name); return(RedirectToAction("Index")); } return(View()); }
public IActionResult EditRole(string user) { var getUser = _appContext.Users.Single(x => x.UserName == user); var userRoles = _userManager.GetRolesAsync(getUser).Result; var role = userRoles.Count == 0 ? "Not set" : userRoles[0]; var roles = _appContext.Roles.Distinct().OrderBy(x => x.Name).Select(p => new SelectListItem() { Value = p.Id, Text = p.Name }).OrderBy(o => o.Text).ToList(); var model = new AdminUserEditRole() { Roles = roles, Role = role, User = getUser }; return(View(model)); }