简体   繁体   English

ASP.NET 核心对象列表返回“空”列表

[英]ASP.NET Core list of objects returns "empty" list

My mehtod returns an empty list:我的方法返回一个空列表: 在此处输入图像描述

Here is my code:这是我的代码:

    [HttpGet]
    [Route("GetDomoticzDevices")]
    public async Task<List<DomoticzDeviceStatus>> GetAsync() {
        KlevebrandContext dbContext = new KlevebrandContext();
        List<DomoticzDeviceStatus> domoticzDeviceStatuses = new List<DomoticzDeviceStatus>();

        foreach(var domoticzDevice in dbContext.TblDomoticzDevices.ToList())
        {
            var response = await client.GetAsync("http://10.0.0.11:8080/json.htm?type=devices&rid=" + domoticzDevice.Idx.ToString());

            domoticzDeviceStatuses.Add(new DomoticzDeviceStatus(domoticzDevice, ((JObject)JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result))["result"][0]["Data"].ToString(), ((JObject)JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result))["result"][0]["LastUpdate"].ToString()));
        }

        return domoticzDeviceStatuses;
    }

In my debugger the "domoticzDeviceStatuses" has 15 objects with set values, but in my browser it is empty.在我的调试器中,“domoticzDeviceStatuses”有 15 个具有设置值的对象,但在我的浏览器中它是空的。

Heres the result in the debugger:这是调试器中的结果:

在此处输入图像描述

There are porpper values in the list.列表中有 porpper 值。

The DomoticzDeviceStatus class looks like this: DomoticzDeviceStatus class 看起来像这样:

public class DomoticzDeviceStatus
{
    TblDomoticzDevice _tblDomoticzDevice;
    string _device_status;
    string _timestamp;

    public DomoticzDeviceStatus(TblDomoticzDevice tblDomoticzDevice, string device_status, string timestamp) { 
        _tblDomoticzDevice = tblDomoticzDevice;
        _device_status = device_status;
        _timestamp = timestamp;
    }
}

If theres any more information you need just tell me:)如果您需要更多信息,请告诉我:)

Thanks in advance!提前致谢! Best regards Max最好的问候最大

Try to change fields in your model to properties.尝试将 model 中的字段更改为属性。

public class DomoticzDeviceStatus
{
    public TblDomoticzDevice TblDomoticzDevice {get; set;}
    public string Device_status {get; set;}
    public string Timestamp {get; set;}

    public DomoticzDeviceStatus(TblDomoticzDevice tblDomoticzDevice, string 
device_status, string timestamp) { 
        TblDomoticzDevice = tblDomoticzDevice;
        Device_status = device_status;
        Timestamp = timestamp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的对象列表呈现为复选框返回一个空列表? [ASP.NET 核心 - 3.1] - Why does my List of Objects rendered as Checkboxes return an Empty List? [ASP.NET Core - 3.1] Fresh ASP.NET Core API 返回空 JSON 对象 - Fresh ASP.NET Core API returns empty JSON objects ASP.NET MVC最小起订量返回空列表 - ASP.NET MVC moq returns empty list 模型的ASP.NET Core MVC List属性在视图中为空 - ASP.NET Core MVC List property of model empty in view ASP.Net Core 3.1 MVC - 带有对象列表的多选 - ASP.Net Core 3.1 MVC - MultiSelect with List of Objects 在 ASP.NET Core 中接受来自表单数据的对象列表 - Accept a list of objects from form data in ASP.NET Core C#asp.net从另一个列表创建字符串列表,该列表返回类对象的列表 - C# asp.net create string list from a another list which returns list of class objects 为什么asp.net核心反序列化json null属性(列表)为空列表 - Why does asp.net core deserialize json null property (list) as an empty list 将多选列表发布到对象列表 asp.net core 2 - post multi-select list to list of objects asp.net core 2 ASP.NET 尝试将对象列表添加到包含列表的 Object 时出现 CORE 错误 - ASP.NET CORE Error when trying to add a List of Objects to an Object with a list in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM