It is not clear how your modal works and there are other unknowns in the code.
A modal is HTML that is shown or hidden. With that in mind I created the following working example. The component shows a list of users with a button that selects the user. Selecting a user shows the use below the line (<hr />) and selects the user's role from the InputSelect. If the user is not in a role then "--Select--" is displayed. Select a role and click the Update User Role button to assign the role. Click the "Select User" button to see what role the user is in. Keep in mind, this is demo code to convey a concept it is not production ready code.
The code also assumes you have the RoleManager configured in Program.cs.
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
Sample
@page "/useradmin/userrolemanager"
@using Microsoft.AspNetCore.Identity;
<h3>User Role Manager</h3>
<div>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Username</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var user in users)
{
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
<td><button @onclick="() => SelectUser(user.Id)">Select User</button></td>
</tr>
}
</tbody>
</table>
</div>
<hr />
<div>
<div>
@selectedUser
</div>
<div>
<InputSelect @bind-Value="userRole">
<option value>--select--</option>
@foreach(var role in roles)
{
<option value="@role.Name">
@role.Name
</option>
}
</InputSelect>
</div>
<div>
<button @onclick="() => UpdateUserRole()">Update User Role</button>
</div>
</div>
@code {
[Inject]
public RoleManager<IdentityRole> roleManager { get; set; }
[Inject]
public UserManager<IdentityUser> userManager { get; set; }
private List<IdentityRole> roles = new List<IdentityRole>();
private List<IdentityUser> users = new List<IdentityUser>();
private string selectedUser { get; set; } = "Select a user";
private string userRole { get; set; }
protected override async Task OnInitializedAsync()
{
roles = roleManager.Roles.ToList();
users = userManager.Users.ToList();
}
private async Task SelectUser(string id)
{
var user = await userManager.FindByIdAsync(id);
selectedUser = user.UserName;
userRole = (await userManager.GetRolesAsync(user)).FirstOrDefault() ?? "";
}
private async Task UpdateUserRole()
{
var user = await userManager.FindByNameAsync(selectedUser);
var uRole = (await userManager.GetRolesAsync(user)).FirstOrDefault();
if (uRole != null)
{
await userManager.RemoveFromRoleAsync(user, uRole);
}
await userManager.AddToRoleAsync(user, userRole);
}
}