编辑用户的 ViewModel:

    1. public class EditUserViewModel
    2. {
    3. //public EditUserViewModel()
    4. //{
    5. // Claims = new List<string>();
    6. // Roles = new List<string>();
    7. //}
    8. public string Id { get; set; }
    9. [Required]
    10. public string UserName { get; set; }
    11. [EmailAddress]
    12. public string Email { get; set; }
    13. public IList<string> Roles { get; set; }
    14. public List<string> Claims { get; set; }
    15. public string City { get; set; }
    16. }

    编辑用户的两个方法:

    1. [HttpGet]
    2. public async Task<IActionResult> EditUser(string id)
    3. {
    4. var user = await _userManager.FindByIdAsync(id);
    5. if (user == null)
    6. {
    7. ViewBag.ErrorMessage = $"无法找到 ID {id} 的用户";
    8. return View("NotFound");
    9. }
    10. var userClaims = await _userManager.GetClaimsAsync(user);
    11. var userRoles = await _userManager.GetRolesAsync(user);
    12. var model = new EditUserViewModel
    13. {
    14. Id = user.Id,
    15. Email = user.Email,
    16. UserName = user.UserName,
    17. City = user.City,
    18. Claims = userClaims.Select(c => c.Value).ToList(),
    19. Roles = userRoles
    20. };
    21. return View(model);
    22. }
    23. [HttpPost]
    24. public async Task<IActionResult> EditUser(EditUserViewModel model)
    25. {
    26. if (ModelState.IsValid)
    27. {
    28. var user = await _userManager.FindByIdAsync(model.Id);
    29. if (user == null)
    30. {
    31. ViewBag.ErrorMessage = $"无法找到 ID {model.Id} 的用户";
    32. return View("NotFound");
    33. }
    34. user.Email = model.Email;
    35. user.UserName = model.UserName;
    36. user.City = model.City;
    37. var result = await _userManager.UpdateAsync(user);
    38. if (result.Succeeded)
    39. {
    40. return RedirectToAction("ListUsers");
    41. }
    42. foreach (var item in result.Errors)
    43. {
    44. ModelState.AddModelError("", item.Description);
    45. }
    46. }
    47. return View(model);
    48. }

    编辑用户的视图:

    1. @model EditUserViewModel
    2. @{
    3. ViewBag.Title = "管理-编辑用户";
    4. }
    5. <h1>编辑用户</h1>
    6. <form method="post" asp-action="EditUser">
    7. <div class="form-group row">
    8. <label asp-for="Id" class="col-sm-2 col-form-label "></label>
    9. <div class="col-sm-10">
    10. <input asp-for="Id" class=" form-control" disabled />
    11. </div>
    12. </div>
    13. <div class="form-group row">
    14. <label asp-for="Email" class="col-sm-2 col-form-label "></label>
    15. <div class="col-sm-10">
    16. <input asp-for="Email" class=" form-control" placeholder="请输入邮箱名称" />
    17. <span asp-validation-for="Email" class="text-danger"></span>
    18. </div>
    19. </div>
    20. <div class="form-group row">
    21. <label asp-for="UserName" class="col-sm-2 col-form-label"></label>
    22. <div class="col-sm-10">
    23. <input asp-for="UserName" class="form-control">
    24. <span asp-validation-for="UserName" class="text-danger"></span>
    25. </div>
    26. </div>
    27. <div class="form-group row">
    28. <label asp-for="City" class="col-sm-2 col-form-label"></label>
    29. <div class="col-sm-10">
    30. <input asp-for="City" class="form-control">
    31. </div>
    32. </div>
    33. <div asp-validation-summary="All" class="text-danger"></div>
    34. <div class="form-group row">
    35. <div class="col-sm-10">
    36. <button type="submit" class="btn btn-primary">更新</button>
    37. <a class="btn btn-primary" asp-action="ListUsers">取消</a>
    38. </div>
    39. </div>
    40. <div class=" card">
    41. <div class="card-header">
    42. <h3>用户角色</h3>
    43. </div>
    44. <div class="card-body">
    45. @if (Model.Roles.Any())
    46. {
    47. foreach (var role in Model.Roles)
    48. {
    49. <h5 class="card-title">@role</h5>
    50. }
    51. }
    52. else
    53. {
    54. <h5 class="card-title">目前没有信息</h5>
    55. }
    56. </div>
    57. <div class="card-footer">
    58. <a href="#" class="btn btn-primary" style="width: auto">管理角色</a>
    59. </div>
    60. </div>
    61. <div class="card mt-3">
    62. <div class="card-header">
    63. <h3>用户声明</h3>
    64. </div>
    65. <div class="card-body">
    66. @if (Model.Claims.Any())
    67. {
    68. foreach (var claim in Model.Claims)
    69. {
    70. <h5 class="card-title">@claim</h5>
    71. }
    72. }
    73. else
    74. {
    75. <h5 class="card-title">目前没有信息</h5>
    76. }
    77. </div>
    78. <div class="card-footer">
    79. <a href="#" class="btn btn-primary" style="width: auto">管理声明</a>
    80. </div>
    81. </div>
    82. </form>

    效果:
    image.png