我在AplpicationController中有一个错误,我不确定如何修复它。我真的很感谢你的帮助
在下面的Index()方法中,我收到了下面这一行的错误: NumberOfUsers = r.Users.Count -- ApplicationRole没有包含用户的定义...
下面是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; }
}
}发布于 2020-03-20 03:59:32
我建议这样做: NumberOfUsers = (r.Users != null)?r.Users.Count :0
您只能对非null的列表执行计数。如果列表为空,则计数为零
https://stackoverflow.com/questions/60723245
复制相似问题