Skip to main content
 首页 » 编程设计

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

2024年02月06日21TianFang

我有一个包含嵌套子类的类:

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 razor View 中,我尝试访问数据:

@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 中的 Device 上,我得到“找不到类型或 namespace 名称“Device””。

如何在我的 View 中访问嵌套类类型?

请您参考如下方法:

您需要指定嵌套名称:

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

或者使用隐式类型:

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

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