“序列化'System.Reflection.RuntimeModule'类型的对象时检测到循环引用”[复制]

时间:2023-01-19 20:58:38

This question already has an answer here:

这个问题在这里已有答案:

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

我试图使用jQuery返回JSON结果中的MVC模型的对象。我收到一条失败消息:

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

序列化“System.Reflection.RuntimeModule”类型的对象时检测到循环引用

This is my controller where i am returing the Json result

这是我的控制器,我正在回顾Json的结果

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:

这里我给Controller方法一个Ajax调用:

$.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 ?

请帮助如何将Model类对象返回到Ajax post调用?

3 个解决方案

#1


6  

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.

我们需要对对象进行搜索,而不是发送Model的直接对象。

Thanks.

#2


2  

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.

当对象的属性具有指向父对象的属性时,就会发生循环引用。它会导致无限循环。在没有看到构成ErrorPage类的细节的情况下,很难告诉你哪个属性对此负责。

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.

但是这种事情的典型解决方案是制作一个与你的类结构相同的ViewModel减去循环引用,或者你可以使用https://json.codeplex.com/,它有一些你可以添加的装饰属性序列化期间忽略的属性。

#3


1  

The error message is very explicit: The object you're tying to serialize (_objError) has a circular reference. This mean the one of it's properties is pointing to it's own instance directly or indirectly. Ex. instance A has a property pointing to instance B which points to instance A (like a parent property).

错误消息非常明确:您要序列化的对象(_objError)具有循环引用。这意味着它的一个属性直接或间接地指向它自己的实例。防爆。实例A具有指向实例B的属性,该实例指向实例A(类似于父属性)。

This is causing the serialization to fail because it would create an infinite loop (A.child = B / B.parent = A / A.child = B / ...). In order to fix this, you'll have to break the circular reference by ignoring the property causing the circular reference or by creating an other object that doesn't has such property.

这导致序列化失败,因为它会创建一个无限循环(A.child = B / B.parent = A / A.child = B / ...)。为了解决这个问题,你必须通过忽略导致循环引用的属性或者创建一个没有这种属性的其他对象来打破循环引用。

#1


6  

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.

我们需要对对象进行搜索,而不是发送Model的直接对象。

Thanks.

#2


2  

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.

当对象的属性具有指向父对象的属性时,就会发生循环引用。它会导致无限循环。在没有看到构成ErrorPage类的细节的情况下,很难告诉你哪个属性对此负责。

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.

但是这种事情的典型解决方案是制作一个与你的类结构相同的ViewModel减去循环引用,或者你可以使用https://json.codeplex.com/,它有一些你可以添加的装饰属性序列化期间忽略的属性。

#3


1  

The error message is very explicit: The object you're tying to serialize (_objError) has a circular reference. This mean the one of it's properties is pointing to it's own instance directly or indirectly. Ex. instance A has a property pointing to instance B which points to instance A (like a parent property).

错误消息非常明确:您要序列化的对象(_objError)具有循环引用。这意味着它的一个属性直接或间接地指向它自己的实例。防爆。实例A具有指向实例B的属性,该实例指向实例A(类似于父属性)。

This is causing the serialization to fail because it would create an infinite loop (A.child = B / B.parent = A / A.child = B / ...). In order to fix this, you'll have to break the circular reference by ignoring the property causing the circular reference or by creating an other object that doesn't has such property.

这导致序列化失败,因为它会创建一个无限循环(A.child = B / B.parent = A / A.child = B / ...)。为了解决这个问题,你必须通过忽略导致循环引用的属性或者创建一个没有这种属性的其他对象来打破循环引用。