"A circular reference was found while serializing an object of type" System.Reflection.RuntimeModule ""

I am trying to return an MVC Model object to a JSON result using jQuery. I get an error like:

A circular reference was found while serializing an object of type "System.Reflection.RuntimeModule"

This is my controller where I save 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 an Ajax call to the 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);
        }
    });

Immediate help on how to return a model class object to an Ajax post post?

+4
source share
3 answers

Here we go to solve

I changed my code using the code below 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 program the object, not send a direct Model object.

Thank.

+4

, , . . , ErrorPage, , .

ViewModel, , , https://json.codeplex.com/, , , .

+2

: , (_objError), . , . Ex. A , B, A ( ).

, (A.child = B/B.parent = A/A.child = B/...). , , , , , .

+1

All Articles