获得客户角色nopcommerce

时间:2013-01-30 18:29:47

标签: c# razor nopcommerce

任何人都可以帮助我在2.8版本的视图中获得客户角色吗?我尝试过workcontext,错误说“当前上下文中不存在workcontext”。我在下面有以下代码,但它不起作用..

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

2 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

你可以使用,

@User.IsInRole("Administrators")