Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 基于用户角色的数据_Asp.net Mvc 3_Roles_User Roles - Fatal编程技术网

Asp.net mvc 3 基于用户角色的数据

Asp.net mvc 3 基于用户角色的数据,asp.net-mvc-3,roles,user-roles,Asp.net Mvc 3,Roles,User Roles,我有一种价格不一的产品。我希望根据用户组显示相应的价格 就目前而言,我创建了描述这些组的角色 管理员 用户价格 用户价格 用户价格 我想以某种方式获得当前用户角色 大概是这样的: public decimal Price { get { var price = Prices.First(p => p.PriceType.Name == Something.CurentUser.Role); return price; } } 可以

我有一种价格不一的产品。我希望根据用户组显示相应的价格

就目前而言,我创建了描述这些组的角色

  • 管理员
  • 用户价格
  • 用户价格
  • 用户价格
  • 我想以某种方式获得当前用户角色

    大概是这样的:

    public decimal Price {
        get
        {
            var price = Prices.First(p => p.PriceType.Name == Something.CurentUser.Role);
            return price; 
        }
    }
    

    可以根据每个角色的名称编写条件语句。根据用户是否是角色的成员,执行不同的LINQ语句

    if (User.IsInRole("Admin")) {
        // ...
    }
    else if (User.IsInRole("UserPriceA")) {
       // ...
    }
    else if (User.IsInRole("UserPriceB")) {
       // ...
    }
    else if (User.IsInRole("UserPriceC")) {
       // ...
    }
    

    您不能这样做,因为一个用户可以有多个角色,但您可以执行Prices.Where(p=>Membership.GetRoles().Contains(p.PriceType.Name)) Prices.Where( p=> Membership.GetRoles().Contains(p.PriceType.Name)) 在这个电话上打字不好玩。。。抱歉这么简单