AnswerBun.com

Asp.net Identity Role in Razor Pages

Stack Overflow Asked on September 7, 2020

I have this working RoleController and its View which list all the roles

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

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. 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. But for Razor Pages version, what should it returns since it has model binding feature?

One Answer

Here is a working demo:

Index.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:

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: enter image description here

Update:

Index.cshtml.cs:

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

Index.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:

@using IdentityRazor3_1.Pages

Or:

@namespace IdentityRazor3_1.Pages

Correct answer by Rena on September 7, 2020

Add your own answers!

Related Questions

How to override CSS class over input[type]?

2  Asked on December 25, 2021 by johnnyd

 

Remove NAs from nested list data frame

2  Asked on December 25, 2021

         

Bulma + Vuetify can’t access scss variables with Vuejs

1  Asked on December 25, 2021 by fabio-paitra

   

Liberty activate @MessageDriven

2  Asked on December 25, 2021 by mah454

         

Traefik 2 Gateway Timeout

1  Asked on December 25, 2021 by casey-daniel

   

Make TabItems Added Through C# Code Resize Automatically

2  Asked on December 25, 2021 by bradley-a

         

Api platform handling fille uploads

2  Asked on December 25, 2021 by andrey-kryukov

       

iTerm2: How to expand split pane temporarily?

3  Asked on December 25, 2021 by pratik-khadloya

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP