Asp. net Роль удостоверения в Razor Pages - PullRequest
0 голосов
/ 04 августа 2020

У меня есть рабочий RoleController и его представление, в котором перечислены все роли.

Класс RoleController:

public class RoleController : Controller
{
    RoleManager<IdentityRole> roleManager;

    public RoleController(RoleManager<IdentityRole> roleManager)
    {
        this.roleManager = roleManager;
    }

    public IActionResult Index()
    {
        var roles = roleManager.Roles.ToList();
        return View(roles);
    }  
}

Просмотр индекса

@model IEnumerable<Microsoft.AspNetCore.Identity.IdentityRole>
@{
    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)
        {
            <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>  

Теперь я хотел бы чтобы перенести этот код на Razor Pages. И вот что я создал:

public class IndexModel : PageModel
{
    RoleManager<IdentityRole> roleManager;

    public IndexModel(RoleManager<IdentityRole> roleManager)
    {
        this.roleManager = roleManager;
    }

    public async Task<IActionResult> OnGetAsync()
    {
        var roles = await roleManager.Roles.ToListAsync();

        return Page();
    }
}

В версии MVC он возвращает roles в представление. Но что должно возвращать для версии Razor Pages, поскольку у нее есть функция привязки модели?

1 Ответ

1 голос
/ 04 августа 2020

Вот рабочая демонстрация:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...