从控制器访问数据并在Identity Razor页面中使用它

时间:2019-06-07 14:56:16

标签: c# .net asp.net-identity identity

我想访问Controllers文件夹中的某些数据并在Identity中使用它们。

我有2个具有相同代码的视图,第一个位于Views文件夹中,第二个位于Identity / Pages / Account / Manage中。第二个无效。

First view

Second view

第一查看代码:

第二个查看代码:

@page
@using HireMe.Models
@using HireMe.ViewModels.Accounts
@using Microsoft.AspNetCore.Identity

@model HireMe.ViewModels.Accounts.UserIdViewModel

@inject UserManager<User> UserManager
@{
    ViewData["Title"] = "User List";
    Layout = "/Areas/Identity/Pages/_Layout.cshtml";

    var index = 1;
}

   
                            @foreach (var user in this.ViewData["Users"] as IEnumerable<UserViewModel>)
        {
                            <tr class="row">
                                <th class="col-md-1">@index</th>
                                <td class="col-md-5">@user.Username</td>
                                <td class="col-md-6">
                                    <div class="form-inline text-center justify-content-around">
                                        @if (await this.UserManager.IsInRoleAsync(this.UserManager.Users.FirstOrDefault(u => u.UserName == user.Username), "Admin"))
                        {
                                        <form asp-controller="Accounts" asp-action="DemoteUser" method="post" class="form-horizontal text-center">
                                            <input type="hidden" asp-for="Id" value="@user.Id" />
                                            <button type="submit" class="btn btn-info text-dark" value="Demote">Demote</button>
                                        </form>
                        }
                        else
                        {
                                        <form asp-controller="Accounts" asp-action="PromoteUser" method="post" class="form-horizontal text-center">
                                            <input type="hidden" asp-for="Id" value="@user.Id" />
                                            <button type="submit" class="btn btn-info text-dark" value="Promote">Promote</button>
                                        </form>
                        }
                                 

0 个答案:

没有答案