0

谁能帮我在 2.8 版的视图中获得客户角色?我已经尝试过工作上下文,并且错误说“当前上下文中不存在工作上下文”。我在下面有以下代码,但它不起作用..

@model BoardsIndexModel
@using Nop.Core.Infrastructure;
@using Nop.Services.Helpers;
@using Nop.Web.Models.Boards;
@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;


@if (Roles.IsUserInRole("Administrators"))
{
    Do that;
}
else
{
    Do this;
}
4

2 回答 2

4

在 nopcommerce 论坛上一个好人的帮助下,我就是这样做的:

@using Nop.Core.Domain.Customers;
@using Nop.Services.Customers;
@using Nop.Core;
@{
    bool customerHasRoleX = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("Administrators");
    bool customerHasRoleY = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsInCustomerRole("ForumModerators");
}

@if ((customerHasRoleX == true) | (customerHasRoleY == true)) {

}
else
{
    Response.Redirect("~/login?ReturnUrl=%2fboards");
}
于 2013-01-31T16:03:59.230 回答
0

您可以使用,

@User.IsInRole("Administrators")
于 2013-01-30T18:34:20.190 回答