无法访问视图模型中的对象属性

时间:2013-12-04 19:25:11

标签: 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 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对象的属性,例如:

public SystemInformation GetSystemInfo(int pagesize) 

        {
            SystemInformation s = new SystemInformation()
            {
                AssetCount.CustomerCount = entities.AccountDefinitions.Count() //I can not access the CustomerCount !!!!

所以有人可以告诉我这是什么问题吗?感谢。

1 个答案:

答案 0 :(得分:9)

您必须先初始化AssetCount属性:

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