c#嵌套类无法找到类型或命名空间

时间:2014-06-09 11:52:59

标签: c# asp.net-mvc class razor

我有一个嵌套子类的类:

public class listDevicesModel
    {
        public int totalCount { get; set; }
        public Messages messages { get; set; }
        public Devices devices { get; set; }

        public class Entry
        {
            public string key { get; set; }
            public object value { get; set; }
        }

        public class Detail
        {
            public List<Entry> entry { get; set; }
        }

        public class Devices
        {
            public List<Device> device { get; set; }
        }

        public class Device
        {
            public string @id { get; set; }
            public string uuid { get; set; }
            public string principal { get; set; }
            public int blockReason { get; set; }
            public int clientId { get; set; }
            public string comment { get; set; }
            public int compliance { get; set; }
            public int countryCode { get; set; }
            public int countryId { get; set; }
            public string countryName { get; set; }
            public string createdAt { get; set; }
            public string currentPhoneNumber { get; set; }
            public List<Detail> details { get; set; }
            public int deviceCount { get; set; }
            public string easLastSyncAttempt { get; set; }
            public string easUuid { get; set; }
            public string emailAddress { get; set; }
            public string emailDomain { get; set; }
            public bool employeeOwned { get; set; }
            public string homeOperator { get; set; }
            public int languageCountryId { get; set; }
            public int languageId { get; set; }
            public string lastConnectedAt { get; set; }
            public string manufacturer { get; set; }
            public bool mdmManaged { get; set; }
            public int mdmProfileUrlId { get; set; }
            public string model { get; set; }
            public string name { get; set; }
            public bool notifyUser { get; set; }
            public string @operator { get; set; }
            public int operatorId { get; set; }
            public string platform { get; set; }
            public string platformType { get; set; }
            public int quarantinedStatus { get; set; }
            public int regCount { get; set; }
            public string regType { get; set; }
            public string registeredAt { get; set; }
            public string status { get; set; }
            public int statusCode { get; set; }
            public string userDisplayName { get; set; }
            public string userFirstName { get; set; }
            public string userLastName { get; set; }
            public int userSource { get; set; }
            public string userUUID { get; set; }
            public int wipeReason { get; set; }
        }
    }

在我的MVC剃刀视图中,我尝试访问数据:

@model PostenNorge.Models.JsonObjectModels.listDevicesModel
<b>@Model.messages.message</b>
<br />
<br />
<ul>

    @foreach(Device d in Model.devices.device)
    {
        <li>@d.name - @d.uuid - @d.currentPhoneNumber</li>
    }

</ul>

在foreach中的设备上我得到“类型或命名空间名称”设备“找不到”。

如何在视图中访问嵌套的类类型?

1 个答案:

答案 0 :(得分:4)

您需要指定嵌套名称:

@foreach(listDevicesModel.Device d in Model.devices.device)

或使用隐式输入:

@foreach(var d in Model.devices.device)

我个人无论如何都要避免在这里使用嵌套类,除非你真的必须这样做。即使你真的必须这样做,我也要将顶级类重命名为遵循正常的命名约定,即以大写字母开头。