I'm writing an api call in dotnet core 3.0 and trying to return several objects. I can return all the objects as long as the information in them is simple. But, when I have an array of objects (that also contain arrays) the result for that object is empty.
For example, in the following code, the first three items (a c# object, a list of strings and a string) all return values, but the list of chartdatasets is empty in the result of the call. It is populated when I step through the code on the server side.
Do I have to do something special to return an array of complex objects?
[HttpGet]
public ActionResult<List<Chart>> GetCharts()
{
List<string> axisLabels = new List<string>() {"CLI","MC","MCAL","PAT","PTP","TP"};
string chartTitle = "Outstanding AR by Fin Group";
ReturnKey retKey = new ReturnKey() {AuthKey = "asdasdas", Message = "OK"};
List<ChartDataSet> dataSetList = new List<ChartDataSet>();
List<double> cData = new List<double>();
cData.Add(37795.59);
cData.Add(16839.71);
cData.Add(144.89);
cData.Add(90);
cData.Add(216829.68);
cData.Add(1764.52);
List<string> cBg = new List<string>();
dataSetList.Add(new ChartDataSet("Outstanding AR", cBg, cData));
return Ok(new {retKey, axisLabels, chartTitle, dataSetList});
}
The result in postman looks like this.
{
"retKey": {
"authKey": "asdasdas",
"message": "OK"
},
"axisLabels": [
"CLI",
"MC",
"MCAL",
"PAT",
"PTP",
"TP"
],
"chartTitle": "Outstanding AR by Fin Group",
"dataSetList": [
{}
]
}
I did not post the definition of the ChartDataSet, but it turns out that is where the problem was. From a discussion with a colleague, we found that the ChartDataSet attributes were not encapsulated. After adding {get;set} to each attribute I wanted to return in the API, it seems to be working.
[Serializable]
public class ChartDataSet {
public string label {get;set;}
public string backgroundColor {get;set;}
public List<double> data {get;set;}
public ChartDataSet(string lbl, string bgcolor, List<double> datavalue)
{
label = lbl;
backgroundColor = bgcolor;
data = datavalue;
}
}