简体   繁体   English

序列化类型为object的对象时检测到循环引用

[英]A circular reference was detected while serializing an object of type

I tried this code in my controller : 我在控制器中尝试了以下代码:

List<ProductListingModels> prom = new List<ProductListingModels>();

prom.Add(new ProductListingModels()
{
    ID = item.ID,
    Name = item.Name,
    DepartmentID = item.DepartmentID.Value,
    BrandID = item.BrandID.Value
});

jr.Data = prom;
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return Json(new
{
    ja = jr.Data,
}, JsonRequestBehavior.AllowGet);

This is my ProductListingModel : 这是我的ProductListingModel:

 public class ProductListingModels:ItemEntityDataContext
 {
   public int ID { get; set; }
   public string Name { get; set; }
   public int DepartmentID { get; set; }
   public int BrandID { get; set; }
 }

It was an error : 这是一个错误:

A circular reference was detected while serializing an object of type. 序列化类型的对象时检测到循环引用。

But if I change from adding the object "prom" to adding something like string or integer, It works well. 但是,如果我从添加对象“ prom”更改为添加诸如字符串或整数之类的东西,则效果很好。 I don't know what problem happen of how to adding my objects. 我不知道如何添加对象会发生什么问题。

Can any one show me the solution. 谁能告诉我解决方案。 Welcome to all your question and answer, Thanks so much. 欢迎来到您所有的问题和答案,非常感谢。

I suspect the problem is with references ItemEntityDataContext superclass might hold to other objects. 我怀疑问题在于引用ItemEntityDataContext超类可能持有其他对象。 It is always a good idea to copy your data to a viewmodel class for passing into views. 将数据复制到viewmodel类以传递到视图中总是一个好主意。 In your case however just use LINQ to select fields into new anonymous type and serialize with json. 但是在您的情况下,只需使用LINQ将字段选择为新的匿名类型并使用json进行序列化即可。 Something like this: 像这样:

jr.Data = prom.Select(p => new 
{ 
    ID = p.ID, 
    Name = p.Name, 
    DepartmentID = p.DepartmentID,
    BrandID = p.BrandID
}).ToArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列化一个类型的对象时检测到循环引用 - Circular reference was detected while serializing an object of type 序列化类型错误的对象时检测到循环引用? - A circular reference was detected while serializing an object of type error? Json在序列化类型的对象时检测到循环引用 - Json A circular reference was detected while serializing an object of type 实体到 json 错误 - 序列化类型对象时检测到循环引用 - Entity to json error - A circular reference was detected while serializing an object of type 序列化类型的对象时检测到循环引用...如何解决? - A circular reference was detected while serializing an object of type… How to solve this? 在序列化“住宅建筑”类型的对象时检测到循环参考 - A circular reference was detected while serializing an object of type Residential Building 为什么我在调用 web 服务时收到此错误,在序列化 object 类型时检测到循环引用? - Why do i get this error while calling a web service that a circular reference was detected while serializing an object of type? JSON.stringify引发错误“序列化类型为&#39;System.Reflection.RuntimeModule&#39;的对象时检测到循环引用”。 ” - JSON.stringify throws error “A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'. ” 序列化类型为&#39;System.Reflection.RuntimeModule&#39;的对象时检测到循环引用 - A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule' 序列化\\ u0027ASP.global_asax类型的对象时检测到循环引用 - A circular reference was detected while serializing an object of type \u0027ASP.global_asax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM