public async Task <IActionResult> EditRole(string id) { var role = await _roleManager.FindByIdAsync(id); if (role == null) { return(RedirectToAction("Index")); } var roleEditViewModel = new RoleEditViewModel { Id = id, RoleName = role.Name, Users = new List <string>() }; var users = await _userManager .Users .AsNoTracking() .ToListAsync(); foreach (var user in users) { if (await _userManager.IsInRoleAsync(user, role.Name)) { roleEditViewModel.Users.Add(user.UserName); } } return(View(roleEditViewModel)); }
public async Task <IActionResult> EditRole(RoleEditViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id); if (role != null) { role.Name = model.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("Index")); } ModelState.AddModelError(string.Empty, "更新角色时出错."); return(View(model)); } return(RedirectToAction("Index")); }