在C#中更新用户后立即分配IdentityRole(Blazor WebAssembly)



问题

我有一个带有Identity的Blazor WebAssembly项目。我扩展了IdentityUser表,并添加了一个Group列。我有一个等待列表页面,在那里我列出了所有没有任何组的用户。当为用户分配一个新组时,我调用RolesController将角色添加到与其组同名的用户中。问题是当RolesController调用时,用户传递的对象仍然没有组。

代码

等待列表.razor:

@if (waitingUsers == null)
{
<p> Loading </p>
}
else
{
foreach (AspNetUsers user in waitingUsers)
{
<div class="container">
<div class="row justify-content-start">
<p> isim: @user.FirstName @user.SurName</p>
<select class="form-control"
@bind="user.Group">
<option value="0">---Please select a group---</option>
@foreach (var group in groups)
{
@if (group.Id == groupId)
{
<option selected value="@group.Id"> @group.GroupName </option>
}
else
{
<option value="@group.Id"> @group.GroupName </option>
}
}
</select>
</div>
</div>
}
@if (waitingUsers.Length > 0)
{
<button onclick="@Update(waitingUsers)"> Update </button>
}
}
@code {
AspNetUsers[] waitingUsers;
public int groupId = 0;
private List<Groups> groups = new List<Groups>();
protected override async Task OnInitializedAsync()
{
waitingUsers = await http.GetFromJsonAsync<AspNetUsers[]>("api/AspNetUsers/noGroup");
groups = await http.GetFromJsonAsync<List<Groups>>("api/groups");
}
async Task Update(AspNetUsers[] users)
{
foreach (AspNetUsers user in users)
{
/* 
at first version Put method was like this:
await http.PutAsJsonAsync($"api/AspNetUsers/newGroup/{user.Id}", user);
*/
http.PutAsJsonAsync($"api/AspNetUsers/newGroup/{user.Id}", user).Wait();

// route to RolesController
await http.PostAsJsonAsync<AspNetUsers>("AddNewRole", user);

await js.InvokeVoidAsync("alert", $"Updated Successfully!");
}
}

如何确保首先分配用户组?等待的方法不起作用。

主要错误是

onclick="@Update(waitingUsers)"

应为@onclick="@(e => Update(waitingUsers)"

这里有一个工作示例:

@page "/"
@foreach (AspNetUsers user in waitingUsers)
{
<div class="container">
<div class="row justify-content-start">
<p> isim: @user.FirstName @user.SurName</p>
<select class="form-control"
@bind="user.Group">
<option value="0">---Please select a group---</option>
@foreach (var group in groups)
{
@if (group.Id == groupId)
{
<option selected value="@group.Id.ToString()"> @group.GroupName </option>
}
else
{
<option value="@group.Id.ToString()"> @group.GroupName </option>
}
}
</select>
</div>
</div>
}
@if (waitingUsers.Count > 0)
{
<button @onclick="Update"> Update </button>
}
<br />
<ul class="list-group">
@foreach (var item in waitingUsers)
{
<li class="list-group-item">@item.FirstName Group: @item.Group</li>
}
</ul>
@code {
class AspNetUsers
{
public string FirstName { get; set; }
public string SurName { get; set; }
public string Group { get; set; }
}
class Groups
{
public int Id { get; set; }
public string GroupName { get; set; }
}
List<AspNetUsers> waitingUsers = new List<AspNetUsers>();
public int groupId = 0;
private List<Groups> groups = new List<Groups>();
protected override async Task OnInitializedAsync()
{
waitingUsers = new List<AspNetUsers>();
waitingUsers.Add(new AspNetUsers { FirstName = "John Doe" });
waitingUsers.Add(new AspNetUsers { FirstName = "Test Test" });
waitingUsers.Add(new AspNetUsers { FirstName = "Mark Jack" });
groups = new List<Groups>();
groups.Add(new Groups { Id = 1, GroupName = "Group 1" });
groups.Add(new Groups { Id = 2, GroupName = "Group 2" });
groups.Add(new Groups { Id = 3, GroupName = "Group 3" });
}
async void Update(MouseEventArgs e)
{
foreach (AspNetUsers user in waitingUsers)
{
}
}
}

相关内容

  • 没有找到相关文章

最新更新