Home > OS >  ASP.NET Core list of objects returns "empty" list
ASP.NET Core list of objects returns "empty" list

Time:11-26

My mehtod returns an empty list: enter image description here

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.

Heres the result in the debugger:

enter image description here

There are porpper values in the list.

The DomoticzDeviceStatus class looks like this:

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

CodePudding user response:

Try to change fields in your model to properties.

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;
    }
}
  • Related