c# - 无法在我的 View 模型中访问对象的属性

标签 c# asp.net asp.net-mvc-4

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

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 !!!!

所以谁能告诉我问题出在哪里?谢谢。

最佳答案

您必须先初始化 AssetCount 属性:

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

关于c# - 无法在我的 View 模型中访问对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384117/

相关文章:

mysql - ASP.net 代码返回打开 session 错误

entity-framework - 映射时转换值

c# - 使用 C# 显示消息后重定向页面

asp.net-mvc-4 - 无法连接到 SQL Server 数据库

c# - 您如何使用 StructureMap v2.6 解析 signalR v2.0

c# - 为什么使用 .NET P/Invoke 调用 libsass 函数会失败?

c# - 识别给定线程的位置

c# - 在 WCF 契约(Contract)中标记弃用的字段

c# - 如何知道控件(或窗口)何时在 WPF 中呈现(绘制)?

c# - 绑定(bind) C#、ASP.net 后在 gridview 中添加新行