0

I am trying to create data for use in a chart.js plot using C# Controller and javascript. My Controller method returns a JSONResult to a javascript function.

public JsonResult GetPlansPerDoc(){
     //Code to grab data from database
     foreach (RadOnc ro in radOncs){
          pm = new CKPlanTypeDistributionPlotModel()
                {
                    DPName = ro.LastName,
                    DPValue = ro.Plans.Count()
                };

                planList.Add(pm);
            }

            JsonResult data = Json(planList, JsonRequestBehavior.AllowGet);

            return data;
 }

Here's the Javascript code calling the Controller method.

$.ajax({
            type: 'get',
            url: "@Url.Action("GetPlansPerDoc", "HomeData")",
            dataType: 'json',
            success: function (rawData) {
                console.log(rawData);
                const data = rawData.DPName;
                console.log(data);

When I console.log the rawData I get an Array message in the web console with the four items in the json result. However, when I try and use one of the fields of the json result I get an "unknown" error in web console. How do I use the json data? ​

physics90
  • 946
  • 3
  • 9
  • 24

1 Answers1

1

Playing around I used the following process. I remembered rawData[0] would give me the first item. SO I tried rawData[0].DPName and it successfully gave me the first name. So I guess I'll just use the following code to extract the elements.

rawData.forEach(dp =>{
     xLabels.push(dp.DPName);
     yData.push(dp.DPValue);
}

Would love to know if there's a more direct way though.

physics90
  • 946
  • 3
  • 9
  • 24