asp.net-core - 无法从剃刀页面中的列表更新用户身份角色
问题描述
我有一个显示角色复选框的剃须刀页面。将在页面加载时检查所选用户拥有的角色。我想要做的是,我希望能够编辑所选用户的角色。但是当我点击更新时,它不会更新。
这是剃须刀页面:
<EditForm Model="@RoleDto" OnValidSubmit="@EditRole">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="card">
<div class="card-header">
<h2>Manage User Roles</h2>
Add/Remove Roles for User / @UserFullname
</div>
<div class="card-body">
@for (int i = 0; i < numOfRoles; i++)
{
<div class="form-check m-1">
<input type="hidden" value="@RoleListModel[i].Id" />
<input type="hidden" value="@RoleListModel[i].Name" />
<input type="checkbox" checked="@RoleListModel[i].Selected" /> @RoleListModel[i].Name
</div>
}
</div>
</div>
<button type="submit" class="btn btn-success btn-block">
Confirm
</button>
@code {
ApplicationRoleDto RoleDto = new ApplicationRoleDto();
private List<ApplicationRoleDto> RoleListModel;
[Parameter] public string Id { get; set; }
[Parameter] public ApplicationUserDto UserDto { get; set; }
[Parameter] public string UserFullname { get; set; }
[Parameter] public int numOfRoles { get; set; }
protected async override Task OnParametersSetAsync()
{
UserDto = await _client.GetFromJsonAsync<ApplicationUserDto>($"api/userroles/{Id}");
UserFullname = UserDto.FullName;
RoleListModel = await _client.GetFromJsonAsync<List<ApplicationRoleDto>>($"api/rolemanager/{Id}");
numOfRoles = RoleListModel.Count();
}
async Task EditRole()
{
await _client.PostAsJsonAsync($"api/rolemanager/{Id}", RoleListModel);
_navManager.NavigateTo($"/userroles/");
}
}
这是控制器:
[HttpPost]
public async Task<IActionResult> Manage(List<ApplicationRoleDto> model, string Id)
{
var user = await _userManager.FindByIdAsync(Id);
if (user == null)
{
NotFound();
}
var roles = await _userManager.GetRolesAsync(user);
var result = await _userManager.RemoveFromRolesAsync(user, roles);
if (!result.Succeeded)
{
Console.WriteLine("Cannot remove user existing roles");
return NotFound();
}
result = await _userManager.AddToRolesAsync(user, model.Where(x => x.Selected).Select(y => y.Name));
if (!result.Succeeded)
{
Console.WriteLine("Cannot add selected roles to user");
return NotFound();
}
return NoContent();
}
我在这里错过了什么吗?
解决方案
推荐阅读
- facebook - 如何使用基本显示 API 获取多个不同的 Instagram 用户最近的帖子
- laravel - Laravel 5.8 - 如何在视图中从控制器转储参数
- c# - 需要一些帮助,将文本框的转换放在哪里
- reactjs - 与 Theme-UI 集成的组件库
- charts - Chart.js 在特定点的自定义工具提示
- javascript - 屏幕导航选项:如何使用翻译
- c++ - 不能在 std::visit 仿函数中使用继承?
- reactjs - 开玩笑 - 酶测试警告:失败的道具类型
- neo4j - 从 Ne04j 和 Cypher 中的查找表创建关系
- amazon-s3 - AWS S3 Glacier 存储恢复到期