本文介绍了序列化“SubSonic.Schema .DatabaseColumn"类型的对象时检测到循环引用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个简单的 JSON 返回,但我遇到了以下问题.

public JsonResult GetEventData(){var data = Event.Find(x => x.ID != 0);返回JSON(数据);}

我收到一个 HTTP 500 异常,如本问题的标题所示.我也试过

var data = Event.All().ToList()

同样的问题.

这是错误还是我的实现?

解决方案

您的对象层次结构中似乎存在 JSON 序列化程序不支持的循环引用.你需要所有的列吗?您可以在视图中仅选取您需要的属性:

返回 Json(new{PropertyINeed1 = data.PropertyINeed1,PropertyINeed2 = data.PropertyINeed2});

这将使您的 JSON 对象更轻巧且更易于理解.如果你有很多属性,AutoMapper 可以用来自动在 DTO 对象和 View 对象之间映射.>

I am trying to do a simple JSON return but I am having issues I have the following below.

public JsonResult GetEventData()
{
    var data = Event.Find(x => x.ID != 0);
    return Json(data);
}

I get a HTTP 500 with the exception as shown in the title of this question. I also tried

var data = Event.All().ToList()

That gave the same problem.

Is this a bug or my implementation?

解决方案

It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer. Do you need all the columns? You could pick up only the properties you need in the view:

return Json(new
{
    PropertyINeed1 = data.PropertyINeed1,
    PropertyINeed2 = data.PropertyINeed2
});

This will make your JSON object lighter and easier to understand. If you have many properties, AutoMapper could be used to automatically map between DTO objects and View objects.

这篇关于序列化“SubSonic.Schema .DatabaseColumn"类型的对象时检测到循环引用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:49