在序列化类型错误的对象时检测到如何解决循环引用。 [英] How to solve circular reference was detected while serializing an object of type error.
问题描述
我有一个列表,每个列表项包含另一个列表。在渲染列时,它会给我错误http://datatables.net/tn/7。我看了很多但没找到任何解决方案。请帮我解决这个问题。
完全错误低于
I am having a list that each list item contain another list. On the time of render the column it will give me error "http://datatables.net/tn/7". I look a lot but not found any solution. Please help me to overcome by this issue.
Full Error is below
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.tblPropertyDocument_C39281F23DAA0C224A777580A8038E1E55D91AF3EBEC9D53EF39EC1E6EAAAD0C'.
我尝试了什么:
实际上我有一个对象列表,在每个对象里面我有另一个列表。当我将数据返回到数据表时,它将生成由于内部列表项而导致的循环错误。我知道为什么会出现这个问题但是没有得到如何解决它。
以下是我的代码:
What I have tried:
Actually I have a list of objects and inside each object i have another list. When i will return data to the datatable it will generate the circular error due for the inner list items. I know why this problem occur but not get how to resolve it.
Below is my code:
var SelectedAllSending = AllSending1.Select(s => new OutgoingCommunicationVM()
{
dDate = s.dDate,
id = s.id,
cDescription = s.tblDDLSelect.cDescription,
cSubject = s.cSubject,
Recipients = (from t in db.tblRecipients where (t.nMessageID == s.id) orderby t.cName select t).Take(3).ToList(),
TotalRecords = totalRecords,
PageCount = GetPageCountUSPS(s.id, s.tblDDLSelect.cDescription),
EstimatedCost = GetCost(s.id, s.tblDDLSelect.cDescription)
}).ToList();
你在我的代码中看到我有一个列表SelectedAllSending,在这个列表中我有一个名字的另一个列表收件人。
当我将值返回到数据表时,它会给我错误。下面是我的代码。
返回Json(new {draw = draw,recordsFiltered = recordsTotal,recordsTotal = recordsTotal,data = SelectedAllSending},JsonRequestBehavior.AllowGet);
对象内部对象在json返回时给出循环错误。或者我如何在数据表中的列表中传递列表。
请帮我解决这个问题。
as you see in my code I have a list "SelectedAllSending" and inside this list I have another list by the name off "Recipients".
When i will return value to the datatable it will give me error. Below is my code.
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = SelectedAllSending }, JsonRequestBehavior.AllowGet);
object inside object gives circular error on json return. Or how i can pass list inside list in datatable.
please help me to overcome this issue.
推荐答案
它对我有用/>
解决圆形参考需要3件事:
1.将[JsonIgnore]应用于导航属性
2.在global.asax
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json。 ReferenceLoopHandling.Ignore;
3.序列化数据
string json = JsonConvert.SerializeObject(gridDataResult。 GridData,Formatting.None);
返回Json(json,JsonRequestBehavior.AllowGet);
It worked for me
3 things require for resolvingcircular reference :
1. Apply [JsonIgnore] to navigational properties
2. In global.asax
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
3. serialize the data
string json = JsonConvert.SerializeObject(gridDataResult.GridData, Formatting.None);
return Json(json, JsonRequestBehavior.AllowGet);
感谢Satya Prakash Swain的回复。
我按照你的建议工作,这段代码解决了循环引用错误,但也使内部列表数据为空。我也需要内部列表数据。
请帮忙!
Thanks Satya Prakash Swain for the response.
I worked according to your suggestion, this code resolve circular reference error but also make the inner list data to null. I need the inner list data as well.
Please help!
这篇关于在序列化类型错误的对象时检测到如何解决循环引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!