代码之家  ›  专栏  ›  技术社区  ›  l_degaray

如何在视图MVC5中检查用户角色

  •  1
  • l_degaray  · 技术社区  · 7 年前

    正如问题所述,我试图在MVC5视图中检查用户对象的角色。基本上,我的视图所做的是列出所有注册用户及其当前角色。

    以下是我的观点:

        @model IEnumerable<IconicMx.CandidateDatabase.Models.ApplicationUser>
    
    @{
        ViewBag.Title = "Talent Center Institute: Usuarios";
        ViewBag.Menu = "Usuarios";
        ViewBag.Submenu = "Lista de usuarios";
        ViewBag.MenuFaClass = "fa-cog";
        ViewBag.InitialPanel = true;
    }
    
    <h2>Lista de usuarios</h2>
    
    <p>
        @Html.ActionLink("Crear usuario", "Register", "Account", null, new { @class = "btn btn-primary" })
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Email)
            </th>
            <th>
                Tipo de usuario
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @if (ViewBag.manager.IsInRole(item.Id, "Admin"))
                {
                    <text>Administrador</text>
                }
                else
                {
                    <text>Capturista</text>
                }
            </td>
            <td>
                @Html.ActionLink("Editar", "Edit", new { id=item.Id }, new { @class = "btn btn-primary btn-xs" }) |
                @Html.ActionLink("Eliminar", "Delete", new { id=item.Id }, new { @class = "btn btn-primary btn-xs" })
            </td>
        </tr>
    }
    
    </table>
    

    这是我的控制器:

     public ActionResult Index()
        {
            ViewBag.manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
            return View(db.Users.ToList());
        }
    

    我正在尝试使用UserManager的IsInRole函数,但在呈现视图时出现运行时错误,表示该函数未定义!!如果在控制器中调用此函数,它将按预期运行。

    注意:我不想在会话中获取当前登录的用户!!(User.identity.IsInRole没有帮助)

    1 回复  |  直到 7 年前
        1
  •  0
  •   trailmax    7 年前

    首先,将数据从controller中的数据库中取出,并将数据传递到视图中,而不是对服务类的引用。这会帮你省去很多麻烦。ViewBag适合通过奇数 可序列化 对象,但在传递对具有大量行为的类的引用方面不是很好。

    其次,您需要使用 IsInRoleAsync method 在UserManager上。是的,有 IsInRole 非异步扩展方法可用,但我看不到您使用了所需的命名空间。

    所以我要做一个视图模型类: 公共类UserViewModel { 公共字符串Id{get;set;} 公共字符串用户名{get;set;} 公共字符串电子邮件{get;set;} 公共bool IsAdmin{get;set;} //其他属性 }

    然后调整控制器以执行数据提取:

    private UserManager<ApplicationUser> userManager;
    
    public MyController()
    {
        userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
    }
    
    public async Task<ActionResult> Index()
    {
        var viewModels = db.Users.Select(u => new UserViewModel(){ Id = u.Id, Username = u.Username, Email = u.Email }).ToList();
        foreach (var userModel in viewModels)
        {
            userModel.IsAdmin = await userManager.IsInRoleAsync(userModel.Id, "Admin");
        }
    
        return View(viewModels);
    }
    

    然后调整你的视角 List<UserViewModel> 并相应地呈现数据-我将让您完成。