C# 无法访问对象';我的视图模型中的属性

C# 无法访问对象';我的视图模型中的属性,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,我添加了以下两个模型视图类: public class AssetCount { public int CustomerCount { get; set; } public int DataCenterCount { get; set; } public int FirewallCount { get; set; } public int RouterCount { get; set; } public in

我添加了以下两个模型视图类:

public class AssetCount
    {
        public int CustomerCount { get; set; }
        public int DataCenterCount { get; set; }
        public int FirewallCount { get; set; }
        public int RouterCount { get; set; }
        public int VirtualMachineCount { get; set; }
        public int ServerCount { get; set; }
        public int StorageDeviceCount { get; set; }
        public int RackCount { get; set; }
        public int SwitchCount { get; set; }
        public int CustomCount { get; set; }
    }


public class SystemInformation
    {

        public AssetCount AssetCount { get; set; }
        public ICollection<TechnologyAudit> TechnologyAudit { get; set; }
        public ICollection<AdminAudit> AdminAudit { get; set; }
        public ICollection<Technology> LatestTechnology { get; set; }
    }

有人能告诉我问题出在哪里吗?谢谢。

您必须首先初始化
AssetCount
属性:

SystemInformation s = new SystemInformation()
{
    AssetCount = new AssetCount { CustomerCount = entities.AccountDefinitions.Count() }
};

您必须首先初始化
AssetCount
属性:

SystemInformation s = new SystemInformation()
{
    AssetCount = new AssetCount { CustomerCount = entities.AccountDefinitions.Count() }
};

尝试将属性名称更改为与类名不同的名称。我假定编译器认为您正在尝试访问静态属性。正如Ufuk所说,您需要初始化它。不仅如此,
AssetCount.CustomerCount
的语法在这样的初始值设定项中是无效的。您可以在
SystemInformation
对象上设置属性,而不能对这些属性执行其他操作(如设置属性)。请尝试将属性名称更改为与类名不同的名称。我假定编译器认为您正在尝试访问静态属性。正如Ufuk所说,您需要初始化它。不仅如此,
AssetCount.CustomerCount
的语法在这样的初始值设定项中是无效的。您可以在
SystemInformation
对象上设置属性,而不能对这些属性执行其他操作(如设置其属性)。