谁能告诉我为什么它无法在我的 EditRole 方法中识别 role.Name?
Can anyone tell me why it wont recognize role.Name in my EditRole method?
我在 RoleManager 中创建了一个名称字段,然后 class 我尝试调用该字段,但它无法识别。它在我看来承认它,所以我不确定发生了什么。我一直在关注本教程 https://www.youtube.com/watch?v=KGIT8P29jf4
[HttpPost]
public async Task<IActionResult> CreateRole(CreateRoleViewModel model)
{
if (ModelState.IsValid)
{
IdentityRole identityRole = new IdentityRole
{
Name = model.RoleName
};
IdentityResult result = await roleManager.CreateAsync(identityRole);
if (result.Succeeded)
{
return RedirectToAction("ListRoles", "Administration");
}
foreach(IdentityError error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
return View(model);
}
[HttpGet]
public IActionResult ListRoles()
{
var roles = roleManager.Roles;
return View(roles);
}
[HttpGet]
public async Task<IActionResult> EditRole(string id)
{
var role = roleManager.FindByIdAsync(id);
if (role == null)
{
ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found";
}
var model = new EditRoleViewModel
{
Id = role.Id,
RoleName = role.Name
};
foreach (var user in userManager.Users)
{
if (await userManager.IsInRoleAsync(user, role.Name))
{
}
}
}
问题是 roleManager.FindByIdAsync 正在返回一个任务。他们需要等待函数调用。
我在 RoleManager 中创建了一个名称字段,然后 class 我尝试调用该字段,但它无法识别。它在我看来承认它,所以我不确定发生了什么。我一直在关注本教程 https://www.youtube.com/watch?v=KGIT8P29jf4
[HttpPost]
public async Task<IActionResult> CreateRole(CreateRoleViewModel model)
{
if (ModelState.IsValid)
{
IdentityRole identityRole = new IdentityRole
{
Name = model.RoleName
};
IdentityResult result = await roleManager.CreateAsync(identityRole);
if (result.Succeeded)
{
return RedirectToAction("ListRoles", "Administration");
}
foreach(IdentityError error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
return View(model);
}
[HttpGet]
public IActionResult ListRoles()
{
var roles = roleManager.Roles;
return View(roles);
}
[HttpGet]
public async Task<IActionResult> EditRole(string id)
{
var role = roleManager.FindByIdAsync(id);
if (role == null)
{
ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found";
}
var model = new EditRoleViewModel
{
Id = role.Id,
RoleName = role.Name
};
foreach (var user in userManager.Users)
{
if (await userManager.IsInRoleAsync(user, role.Name))
{
}
}
}
问题是 roleManager.FindByIdAsync 正在返回一个任务。他们需要等待函数调用。