首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >核心标识- ApplicationController错误

核心标识- ApplicationController错误
EN

Stack Overflow用户
提问于 2020-03-17 21:18:23
回答 1查看 21关注 0票数 1

我在关注一座纪念碑: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没有包含用户的定义...

下面是ApplicationController类:

代码语言:javascript
复制
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

代码语言:javascript
复制
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; }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-03-20 03:59:32

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

您只能对非null的列表执行计数。如果列表为空,则计数为零

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60723245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档