Serialization error while loading google chart Charts

Asked

Viewed 22 times

0

I have the method JsonResult called "Carregarchart" which returns a DataTable for a request Ajax where I’m trying to popular data on a google graphic Maps, and the problem is that the following error occurs:
A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.

Doubt: How to get around this error and load the chart correctly ?

public JsonResult CarregarChart(DashboardSaleBySupervisor model)
        {
            string mensagem = string.Empty;
            try
            {
                DataTable dt1 = new DataTable();
                dt1.Columns.Add("Year", typeof(int));
                dt1.Columns.Add("Sales", typeof(int));
                dt1.Columns.Add("Expenses", typeof(int));
                dt1.Columns.Add("Profit", typeof(int));

                dt1.Rows.Add(2014, 1000, 400, 200);
                dt1.Rows.Add(2015, 1170, 460, 250);
                dt1.Rows.Add(2016, 660, 1120, 300);
                dt1.Rows.Add(2017, 1030, 540, 350);

                return Json(new { success = true, message = mensagem, list = dt1 }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Error(ex);
                return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

AJAX:

function CarregarChart(url) {

    $.ajax({
        url: url,
        type: "POST",
        data: null,
        contentType: "application/json; charset=utf-8",
        processData: false,
        success: function (response) {

                var list = response.list;
                google.charts.load('current', { 'packages': ['bar'] });
                google.charts.setOnLoadCallback(drawChart);

                function drawChart() {
                    var data = google.visualization.arrayToDataTable(list);
                    var options = {
                        chart: {
                            title: 'Company Performance',
                            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
                        }
                    };
                    var chart = new google.charts.Bar(document.getElementById('Chart'));
                    chart.draw(data, google.charts.Bar.convertOptions(options));
                }

        }
    });
};
  • DataTable? to use such a complex object just to return some values, it would make more sense a class with the 4 properties and resume a List of that class

  • @Ricardo Punctual thank you for the comment but this is not the target of the post, this code is an example I used to make the code cleaner and easier to understand because in practice the Database will return a number of different columns to each call dynamically due to the use of the Pivot command so I chose to use the Date objectTable .

  • 1

    "this code is an example I used to make the code cleaner" in this case becomes more difficult to help, need to have an idea of how the real code is. With this code the error happens? I believe you know what the circular reference error means when doing serialize, so the problem is related to the serialization of this Datatable, so it seems to me that my suggestion, at least to do a debug, is very pertinent :)

  • As Ricardo commented, if the problem is not in the present code, this error cannot be reproduced. Without seeing the real code you can’t guess where the error is... Actually the error gives yes, but without seeing the real structure can’t help

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.