RolesInUserViewModel:

    1. namespace StudentManagement.ViewModels
    2. {
    3. public class RolesInUserViewModel
    4. {
    5. public string RoleId { get; set; }
    6. public string RoleName { get; set; }
    7. public bool IsSelected { get; set; }
    8. }
    9. }

    用户角色管理方法:

    1. [HttpGet]
    2. public async Task<IActionResult> ManageUserRoles(string userId)
    3. {
    4. ViewBag.userId = userId;
    5. var user = await _userManager.FindByIdAsync(userId);
    6. if (user == null)
    7. {
    8. ViewBag.ErrorMessage = $"无法找到 Id {userId} 的用户";
    9. return View("NotFound");
    10. }
    11. var model = new List<RolesInUserViewModel>();
    12. foreach (var role in _roleManager.Roles)
    13. {
    14. var rolesInUserViewModel = new RolesInUserViewModel
    15. {
    16. RoleId = role.Id,
    17. RoleName = role.Name,
    18. IsSelected = await _userManager.IsInRoleAsync(user, role.Name)
    19. };
    20. model.Add(rolesInUserViewModel);
    21. }
    22. return View(model);
    23. }
    24. [HttpPost]
    25. public async Task<IActionResult> ManageUserRoles(List<RolesInUserViewModel> model, string userId)
    26. {
    27. var user = await _userManager.FindByIdAsync(userId);
    28. if (user == null)
    29. {
    30. ViewBag.ErrorMessage = $"无法找到 Id {userId} 的用户";
    31. return View("NotFound");
    32. }
    33. var roles = await _userManager.GetRolesAsync(user);
    34. // 移除当前用户中的所有角色信息
    35. var removeRolesResult = await _userManager.RemoveFromRolesAsync(user, roles);
    36. if (!removeRolesResult.Succeeded)
    37. {
    38. ModelState.AddModelError("", "无法删除用户中的现有角色");
    39. return View(model);
    40. }
    41. // 查询出模型列表中选中的 RoleName 添加到用户中
    42. var addRolesResult = await _userManager.AddToRolesAsync(user, model.Where(x => x.IsSelected).Select(y => y.RoleName));
    43. if (!addRolesResult.Succeeded)
    44. {
    45. ModelState.AddModelError("", "无法向用户中添加选定的角色");
    46. return View(model);
    47. }
    48. return RedirectToAction("EditUser", new { Id = userId });
    49. }

    用户角色管理视图:

    1. @model List<RolesInUserViewModel>
    2. @{
    3. var userId = ViewBag.userId;
    4. }
    5. <form method="post">
    6. <div class="card">
    7. <div class="card-header">
    8. <h2>管理用户中的角色</h2>
    9. </div>
    10. <div class="card-body">
    11. @for (var i = 0; i < Model.Count; i++)
    12. {
    13. <div class="form-check m-1">
    14. <input type="hidden" asp-for="@Model[i].RoleId" />
    15. <input type="hidden" asp-for="@Model[i].RoleName" />
    16. <input asp-for="@Model[i].IsSelected" class="form-check-input" />
    17. <label class="form-check-label" asp-for="@Model[i].IsSelected">
    18. @Model[i].RoleName
    19. </label>
    20. </div>
    21. }
    22. <div asp-validation-summary="All" class="text-danger"></div>
    23. </div>
    24. <div class="card-footer">
    25. <input type="submit" value="更新" class="btn btn-primary" style="width: auto" />
    26. <a class="btn btn-primary" style="width: auto" asp-action="EditUser" asp-route-id="@userId">取消</a>
    27. </div>
    28. </div>
    29. </form>

    效果:
    image.png