核心身份-ApplicationController错误

时间:2020-03-17 13:18:23

标签: asp.net-core asp.net-identity

我正在跟踪: https://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/

我在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; }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议这样做: NumberOfUsers =(r.Users!= null)? r.Users.Count:0

您只能对不为空的列表进行计数。如果列表为空,则计数为零