步骤

  1. 准备编辑角色用的 ViewModel

    1. public class EditRoleViewModel
    2. {
    3. [Display(Name = "角色Id")]
    4. public string Id { get; set; }
    5. [Required]
    6. [Display(Name = "角色名称")]
    7. public string RoleName { get; set; }
    8. public List<string> Users { get; set; }
    9. public EditRoleViewModel()
    10. {
    11. Users = new List<string>();
    12. }
    13. }
  2. EditRole Get 方法,通过传入的 Id 返回角色编辑相关信息

    1. [HttpGet]
    2. public async Task<IActionResult> EditRole(string id)
    3. {
    4. var role = await _roleManager.FindByIdAsync(id);
    5. if (role == null)
    6. {
    7. ViewBag.ErrorMessage = $"角色 Id 为 {id} 的信息不存在,请重试。";
    8. return View("NotFound");
    9. }
    10. var model = new EditRoleViewModel
    11. {
    12. Id = role.Id,
    13. RoleName = role.Name
    14. };
    15. var users = _userManager.Users.ToList();
    16. foreach (var user in users)
    17. {
    18. // 如果用户拥有此角色,请讲用户名添加到 EditRoleViewModel 的 Users 属性中
    19. if (await _userManager.IsInRoleAsync(user, role.Name))
    20. {
    21. model.Users.Add(user.UserName);
    22. }
    23. }
    24. return View(model);
    25. }
  3. 编辑角色视图 ```html @model EditRoleViewModel

@{ ViewBag.Title = “编辑角色”; }

编辑角色

  1. <div class="form-group row">
  2. <label asp-for="RoleName" class="col-sm-2 col-form-label"></label>
  3. <div class="col-sm-10">
  4. <input asp-for="RoleName" class="form-control" placeholder="请输入角色名称" />
  5. <span asp-validation-for="RoleName" class="text-danger"></span>
  6. </div>
  7. </div>
  8. <div asp-validation-summary="All" class="text-danger"></div>
  9. <div class="form-group row">
  10. <div class="col-sm-10">
  11. <button type="submit" class="btn btn-primary" style="width: auto">更新</button>
  12. <a class="btn btn-primary" asp-action="ListRoles">取消</a>
  13. </div>
  14. </div>
  15. <div class="card">
  16. <div class="card-header">该角色中的用户</div>
  17. <div class="card-body">
  18. @if (Model.Users.Any())
  19. {
  20. foreach (var user in Model.Users)
  21. {
  22. <h5 class="card-title">@user</h5>
  23. }
  24. }
  25. else
  26. {
  27. <h5 class="card-title">目前没有信息</h5>
  28. }
  29. </div>
  30. <div class="card-footer">
  31. <a class="btn btn-primary" style="width: auto" asp-action="CreateRole" asp-controller="Admin">添加用户</a>
  32. <a class="btn btn-danger" style="width: auto" asp-action="CreateRole" asp-controller="Admin">删除用户</a>
  33. </div>
  34. </div>

  1. 4. 更新角色信息
  2. ```csharp
  3. [HttpPost]
  4. public async Task<IActionResult> EditRole(EditRoleViewModel model)
  5. {
  6. var role = await _roleManager.FindByIdAsync(model.Id);
  7. if (role == null)
  8. {
  9. ViewBag.ErrorMessage = $"角色 Id 为 {model.Id} 的信息不存在,请重试。";
  10. return View("NotFound");
  11. }
  12. role.Name = model.RoleName;
  13. var result = await _roleManager.UpdateAsync(role);
  14. if (result.Succeeded)
  15. {
  16. return RedirectToAction("ListRoles");
  17. }
  18. foreach (var error in result.Errors)
  19. {
  20. ModelState.AddModelError("", error.Description);
  21. }
  22. return View(model);
  23. }

效果

image.png