1

My method 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 proper 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 :)

2
  • Change fields to properties Commented Nov 25, 2022 at 21:19
  • How do you mean? Commented Nov 25, 2022 at 21:19

1 Answer 1

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.