Вот рабочая демонстрация:
Index.cs html:
@page
@model IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>List of Roles</h1>
<a asp-action="Create">Create</a>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
<td></td>
</tr>
</thead>
<tbody>
@foreach (var role in Model.roles)
{
<tr>
<td>@role.Id</td>
<td>@role.Name</td>
<td>
<form asp-action="Delete" asp-route-id="@role.Id" method="post">
<button type="submit" class="btn btn-sm btn-danger">
Delete
</button>
</form>
</td>
</tr>
}
</tbody>
</table>
Index.cs html .cs:
public class IndexModel : PageModel
{
RoleManager<IdentityRole> roleManager;
public IndexModel(RoleManager<IdentityRole> roleManager)
{
this.roleManager = roleManager;
}
public List <IdentityRole> roles { get; set; }
public async Task<IActionResult> OnGetAsync()
{
roles = await roleManager.Roles.ToListAsync();
return Page();
}
}
Результат : введите описание изображения здесь
Обновление:
Index.cs html .cs:
namespace IdentityRazor3_1.Pages
{
public class IndexModel : PageModel
{
//...
}
}
Index.cs html:
@page
@model IdentityRazor3_1.Pages.IndexModel
Если вы не хотите указывать одно и то же пространство имен каждый раз, вы можете добавить следующий код в свой _ViewImports.cshtml
:
@using IdentityRazor3_1.Pages
Или:
@namespace IdentityRazor3_1.Pages