我想獲取分配給某個角色的用戶數(shù)量,如下所示,參見圖片我也檢查了這篇文章,但沒有幫助我。我的角色視圖模型public class RolesViewModel{ public RolesViewModel() { } public RolesViewModel(ApplicationRole role) { Id = role.Id; RoleName = role.Name; } public string Id { get; set; } [Required] [Display(Name = "Role Name")] public string RoleName { get; set; } public int Count { get; set; }}索引視圖 @model IEnumerable<CMSRPPF.Areas.Admin.Models.RolesViewModel>@{ ViewBag.Title = "Index"; Layout = "~/Areas/Admin/Views/Shared/_LayoutDashboard.cshtml";}<h2>Roles</h2><p> @Html.ActionLink("Add Role", "Create") </p><table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.RoleName) </th> <th> @Html.DisplayNameFor(model => model.Count) </th> <th></th> </tr>@foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.RoleName) </td> <td> @Html.DisplayFor(modelItem => item.Count) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | @Html.ActionLink("Details", "Details", new { id = item.Id }) | @Html.ActionLink("Delete", "Delete", new { id = item.Id }) </td> </tr>}</table>控制器 public ActionResult Index(){ ApplicationDbContext db = new ApplicationDbContext(); List<RolesViewModel> list = new List<RolesViewModel>(); foreach (var role in RoleManager.Roles) { var r = new RolesViewModel(role) { RoleName = role.Name, Count = db.Users.Where(x=>x.Roles.Select(roles => roles.RoleId).Contains("4738a87a-4461-4b54-828b-1c5543f13bb2")).ToList().Count() }; list.Add(r); } return View(list);}到目前為止,我成功地根據(jù)特定角色的 RoleId 對用戶進行了計數(shù),但我不知道如何對所有用戶執(zhí)行此操作。如果有人可以幫助我,我將非常感激。
如何獲取某個角色的用戶數(shù)量
慕尼黑5688855
2023-08-20 15:00:51