【问题标题】:A circular reference was detected while serializing an object of type in asp.net mvc c#在 asp.net mvc c# 中序列化类型对象时检测到循环引用
【发布时间】:2012-09-26 10:52:12
【问题描述】:

我尝试搜索但无法解决我的问题。

这是我的控制器:

    public JsonResult Index()
    {
        return this.Json(TempData["displayList"], JsonRequestBehavior.AllowGet);
    }
    public JsonResult AddToCart(string id)
    {
        QuotationModels objQuote = new QuotationModels();

        List<QuotationModels> listQuote = objQuote.GetObjectInSession;

        int itemID = Int32.Parse(id);

        int index = -1;
        if(listQuote != null)
           index = listQuote.FindIndex(p => p.ItemdID== itemID);

        if (index >= 0)
        {
            listQuote[index].ProductQty++;
            objQuote.AddToSession(listQuote);
        }
        else
        {
            int _id = Convert.ToInt16(id);
            var _product = DataContext.DataContext.Items.FirstOrDefault(s => s.ID == _id);

            QuotationModels quote = new QuotationModels();
            quote.ItemdID = _product.ID;
            quote.ItemNote = _product.Notes;
            quote.Phone = "";
            quote.PictureName = _product.PictureName;
            quote.ProductName = _product.Name;
            quote.ProductPrice = (decimal)_product.Price;
            quote.ProductQty = 1;
            quote.ShoppingCartId = "";
            quote.Total = 0;
            quote.Email = "";
            quote.CustomerID = 0;
            quote.CusName = "";
            quote.Company = "";
            quote.Address = "";
            objQuote.AddToSession(quote);

        }

        int itemInSession = objQuote.GetObjectInSession.Sum(s => s.ProductQty);
        TempData["displayList"] = objQuote.GetObjectInSession;

        return Json(new
        {
           ja = itemInSession
        }, JsonRequestBehavior.AllowGet);

    }

但是当我浏览 Index() 视图时,这是一个错误A circular reference was detected while serializing an object of type

谁能给我解决办法。非常感谢。

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    我不确定你要序列化什么,但这里有一个关于你必须做什么的线索!

    循环引用的问题意味着您有例如一个引用,其中包含对产品的引用。并且该 Product itsel 引用了父 Quote。

    你不能简单地用 JSON 序列化它,因为它会做这样的事情:

    Quote :
     - some attributes..
     - Product
       - some attributes
       - Quote 
         - some attributes...
         - Product... and so on !
    

    但是现在,如果您有父引用,那么在子实体上建立关系对您来说真的很有趣吗?也许在您的服务器端模型中,但它有点多余,对客户端来说不是必需的。

    在这种情况下,您可以做的是创建其他类,这些类不包含不必要的内容,并且可能导致循环引用。

    例如,您将拥有一个 SimpleQuote,它类似于一个 Quote,但它不是具有 Product 属性,而是具有 SimpleProduct 属性。

    原始课程:

    public class Quote{
        public int Id;
        public Product Product;
        public double Amount;
    }
    
    public class Product{
        public int Id;
        public Quote Quote;
        public string Name;
    }
    

    用于序列化的简单类:

    public class SimpleQuote{
        public int Id;
        public SimpleProduct Product;
        public double Amount;
    }
    
    public class SimpleProduct{
        public int Id;
        public int QuoteId;
        public string Name;
    
    }
    

    现在您可以创建扩展方法,例如:

    public static void ToSimpleQuote(this Quote quote){
        return new SimpleQuote(){ 
            Id = quote.Id,
            Amount = quote.Amount,
            Product = quote.Product.ToSimpleProduct()
        };
    }
    
    public static void ToSimpleProduct(this Product product){
        return new SimpleProduct(){
            Id = product.Id,
            Name = product.Name,
            QuoteId = product.Quote.QuoteID
        };
    }
    

    所以如果你有类似的东西:

    public ActionResult Index(){
        ....
        return Json(myQuote, JsonRequestBehavior.AllowGet);
    }
    

    你现在可以这样做了:

    public ActionResult Index(){
        ....
        return Json(myQuote.ToSimpleQuote(), JsonRequestBehavior.AllowGet);
    }
    

    【讨论】:

      【解决方案2】:

      不要返回this。而是使用:

       public JsonResult Index()
          {
              return Json(TempData["displayList"], JsonRequestBehavior.AllowGet);
          }
      

      你能显示TempData["displayList"]中的对象图吗?

      你可能有一个循环的层次结构。

      【讨论】:

      • 对不起,Object Graph 在这里是什么意思?但实际上,我还没有在我看来循环它。
      • @socheata 你在 TempData["displayList"] 中放了什么?你能显示代码和其中的任何嵌套对象吗?具有 Phone、PictureName 等的对象
      • @socheata 你试过删除这个吗? TempData 也使用会话,如果您像在 AddCart 中一样再次直接从会话中读取 objQuote.GetObjectInSession 会不会更清楚(显然不会像在 AddCart 中那样更新任何内容)
      【解决方案3】:

      您可以将属性 XMLIgnore 用于严格的 XML 序列化或将 NonSerialized 用于更广泛的目标。

      只需使用这些属性中的任何一个标记循环的属性或字段,它就应该忽略它们。

      在具有确实是循环引用的虚拟成员属性的实体框架场景中运行良好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        • 2012-05-04
        • 2012-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多