Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'" [duplicate]

I am trying to return the object of MVC Model in JSON result using jQuery. I am getting a failure message as:

A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'

This is my controller where i am returing the Json result

public ActionResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
    {

        ErrorPage _objError = new ErrorPage();
        _objError.ErrorData = dbl.GetDataTable(DbConnectionString, Table, whereCondition, columns);


        //Column description: Name and Type    
        var columnlist = new Dictionary<string, System.Type>();
        foreach (System.Data.DataColumn column in _objError.ErrorData.Columns)
        {       
            var t = System.Type.GetType( column.DataType.FullName );
            columnlist.Add(column.ColumnName, t);  
        }

        _objError.ErrorColumns = columnlist;


        //DataSourceRequest result = _objError.ToDataSourceResult(request);

        if (_objError.ErrorData.Rows.Count > 0)
            Message = "Showing Error log for " + AppName + " . To Change the application or filtering options please select the appropriate application from Application Dropdown";
        else
            Message = "No errors found for " + AppName + " in last 24 hours.";


        return Json(_objError);
    }

Here i am giving a Ajax call to Controller method:

$.ajax({
        type: "POST",
        url: '@Url.Content("~/Common/PopulateData")',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify({ application: app, columns: columns, machine: machine, pages: pages, startDate: startDate, endDate: endDate }),
        success: function (data) {
            alert("Success");
        },
        error: function (error) {
            alert('error; ' + eval(error));
            alert('error; ' + error.responseText);
        }
    });

Kindly help how to return a Model class object to Ajax post call ?

like image 926
Anand Avatar asked Dec 14 '22 13:12

Anand


2 Answers

Here we go for Solution

I modified my code with below set of code and it worked for me

public JsonResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
    {
        ErrorPage _objError = new ErrorPage();
        var ErrorResult = _objError.GetErrorData(application, columns, machine, pages, startDate, endDate);


        var result = JsonConvert.SerializeObject(ErrorResult.ErrorData, Formatting.Indented,
                       new JsonSerializerSettings
                       {
                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                       });

        return Json(result, JsonRequestBehavior.AllowGet);
    }

We need to serailize the object rather than sending a direct object of Model.

Thanks.

like image 150
Anand Avatar answered Dec 17 '22 01:12

Anand


A circular reference happens when a property of an object has a property of its own that points back to the parent. It causes an infinite loop. Without seeing the details of what makes up the class ErrorPage, it'll be tough to tell you which property is responsible for this.

But typical solutions for this kind of thing are to either make a ViewModel which is identical to your class structure minus the circular references, or you could use https://json.codeplex.com/ which has some decoration attributes you can add to have a property ignored during serialization.

like image 35
Kevin Heidt Avatar answered Dec 17 '22 01:12

Kevin Heidt