简体   繁体   English

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

[英]Asp.net Identity Role in Razor Pages

I have this working RoleController and its View which list all the roles我有这个工作 RoleController 及其列出所有角色的视图

RoleController class:角色控制器 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);
    }  
}

Index View索引视图

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

Now, I would like to migrate these code to Razor Pages.现在,我想将这些代码迁移到 Razor 页面。 And this is what I have created:这就是我创建的:

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();
    }
}

In MVC version, it returns roles to the View.在 MVC 版本中,它将roles返回给视图。 But for Razor Pages version, what should it returns since it has model binding feature?但是对于 Razor Pages 版本,它应该返回什么,因为它具有 model 绑定功能?

Here is a working demo:这是一个工作演示:

Index.cshtml:索引.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>

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

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

Update:更新:

Index.cshtml.cs:索引.cshtml.cs:

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

Index.cshtml:索引.cshtml:

@page
@model IdentityRazor3_1.Pages.IndexModel

If you do not want to specify the same namespace every time,you could add the following code to your _ViewImports.cshtml :如果您不想每次都指定相同的命名空间,您可以将以下代码添加到您的_ViewImports.cshtml

@using IdentityRazor3_1.Pages

Or:或者:

@namespace IdentityRazor3_1.Pages

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

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