繁体   English   中英

Asp.net Razor 页面中的身份角色

[英]Asp.net Identity Role in Razor Pages

我有这个工作 RoleController 及其列出所有角色的视图

角色控制器 class:

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 页面。 这就是我创建的:

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 版本,它应该返回什么,因为它具有 model 绑定功能?

这是一个工作演示:

索引.cshtml:

@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>

索引.cshtml.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();
    }
}

结果: 在此处输入图像描述

更新:

索引.cshtml.cs:

namespace IdentityRazor3_1.Pages
{
    public class IndexModel : PageModel
    {
         //...
    }
}

索引.cshtml:

@page
@model IdentityRazor3_1.Pages.IndexModel

如果您不想每次都指定相同的命名空间,您可以将以下代码添加到您的_ViewImports.cshtml

@using IdentityRazor3_1.Pages

或者:

@namespace IdentityRazor3_1.Pages

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM