[英]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.