我在AplpicationController中有一个错误,不确定如何解决。 我非常感谢您的帮助
在下面的Index()方法中,我在此行上收到错误: NumberOfUsers = r.Users.Count-ApplicationRole不包含Users的定义...
这是ApplicationController类:
public IActionResult Index()
{
List<ApplicationRoleListViewModel> model = new List<ApplicationRoleListViewModel>();
model = roleManager.Roles.Select(r => new ApplicationRoleListViewModel
{
RoleName = r.Name,
Id = r.Id,
Description = r.Description,
NumberOfUsers = r.Users.Count
}).ToList();
return View(model);
这是ApplicationRoleListViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoreRole.Models
{
public class ApplicationRoleListViewModel
{
public string Id { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public int NumberOfUsers { get; set; }
}
}
答案 0 :(得分:0)
我建议这样做: NumberOfUsers =(r.Users!= null)? r.Users.Count:0
您只能对不为空的列表进行计数。如果列表为空,则计数为零