C# 根据用户过滤选择列表';角色

C# 根据用户过滤选择列表';角色,c#,asp.net-mvc,C#,Asp.net Mvc,将用户分配给某个项目时,我希望将可用用户列表限制为具有“项目经理”角色的人员。我需要一些语法方面的帮助,因为我似乎无法为Where方法获得正确的LINQ语法 创建操作(GET) 创建视图 <div class="form-group"> @Html.LabelFor(model => model.AssignedToId, "Choose a Project Manager", htmlAttributes: new { @class = "control-label

将用户分配给某个项目时,我希望将可用用户列表限制为具有“项目经理”角色的人员。我需要一些语法方面的帮助,因为我似乎无法为Where方法获得正确的LINQ语法

创建操作(GET)

创建视图

<div class="form-group">
     @Html.LabelFor(model => model.AssignedToId, "Choose a Project Manager", htmlAttributes: new { @class = "control-label col-md-2" })
         <div class="col-md-10">
             @Html.DropDownList("AssignedToId", null, htmlAttributes: new { @class = "form-control" })
             @Html.ValidationMessageFor(model => model.AssignedToId, "", new { @class = "text-danger" })
         </div>
</div>

@LabelFor(model=>model.AssignedToId,“选择项目经理”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“AssignedToId”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.AssignedToId,“,new{@class=“text danger”})
我在DropDownList上看到一个错误

无法比较“System.Collections.Generic.ICollection”类型的元素。仅支持基元类型、枚举类型和实体类型


如果
角色
是一个集合,那么我建议在LINQ to Entities查询之外创建
项目经理
列表。此外,创建一个
常量字符串
变量来保存
“Project Manager”
魔术字符串。这将使您不必在使用该字符串的每个位置更改该字符串(如果该字符串发生更改)

public const string roleProjectManager = "Project Manager";

var lstProjectManagers = db.Users.Where(x => x.Roles.Contains(roleProjectManager)).ToList();
然后创建您的选择列表

ViewBag.AssignedToId= new SelectList(lstProjectManagers, "Id", "FullName");

让我知道这是否有帮助

假设这是您正在使用的实体框架,您会注意到db.Users是DbSet类型,而db.Users.Where(…)是IQueryable类型。我可以想象,如果您添加.ToList(),您将获得所需的结果,如下所示:

ViewBag.AssignedToId= new SelectList(db.Users.Where(c => c.Roles.Contains("Project Manager")).ToList(), "Id", "FullName");

我的问题的解决方案是在控制器创建操作(GET)中包含以下代码:

而不是:

ViewBag.AssignedToId = new SelectList(db.Users, "Id", "FullName");
然后将其包括在“创建”视图中:

<div class="form-group">
      @Html.LabelFor(model => model.AssignedToId, "Choose a Project Manager", htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
           @Html.DropDownListFor(Model => Model.AssignedToId, ViewBag.AssignedId as SelectList, null, htmlAttributes: new { @class = "form-control" })
           @Html.ValidationMessageFor(model => model.AssignedToId, "", new { @class = "text-danger" })
      </div>
</div>

我假设
角色
是一个
列表
。我得到的字符串错误与另一个答案中的错误相同:无法从字符串转换为Microsoft.AspNet.Identity.EntityFramework.IdentityUserRoleNo这不是一个选项,只有Count&IsReadOnly属性可用
用户
对应于数据库中的哪个表?在服务器资源管理器中,我假设它对应于AspNetUsers表,我在字符串“Project Manager”上看到一个红色的波形,表示“无法从字符串转换为Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole”,你知道如何解决这个问题吗?@springath这意味着
Roles
属于
IdentityUserRole
类型,你试图将其与字符串进行比较<代码>“项目经理”是
角色的集合还是
角色的集合
对象的集合?我很确定这些角色只是一个字符串,尽管它们有自己的名为dbo.AspNetRoles的表,其中包含Id列和角色列
ViewBag.AssignedToId = new SelectList(db.Users, "Id", "FullName");
<div class="form-group">
      @Html.LabelFor(model => model.AssignedToId, "Choose a Project Manager", htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
           @Html.DropDownListFor(Model => Model.AssignedToId, ViewBag.AssignedId as SelectList, null, htmlAttributes: new { @class = "form-control" })
           @Html.ValidationMessageFor(model => model.AssignedToId, "", new { @class = "text-danger" })
      </div>
</div>
@Html.DropDownList("AssignedToId", null, htmlAttributes: new { @class = "form-control" })