【问题标题】:A circular reference was detected while serializing an object of type in AngularJs and asp.net mvc在 AngularJs 和 asp.net mvc 中序列化类型对象时检测到循环引用
【发布时间】:2017-08-30 06:34:33
【问题描述】:

我知道这是众所周知的错误。这里有很多问题。但是在经历了几个问题之后,我无法解决我的问题,我的网站出现了这个错误。

这是我的错误 序列化类型的对象时检测到循环引用

我的控制器代码是

[HttpGet]
        public JsonResult Get(int currentPage, int recordsPerPage)
        {
            var pageNumber = currentPage;
            var pageSize = recordsPerPage;
            var begin = (pageNumber - 1) * pageSize;

            var totalNumberOfRecords = db.Products.Count();
            var productlist = db.Products.OrderBy(r => r.ProductID).Skip(begin).Take(pageSize).ToList();
            db.Configuration.ProxyCreationEnabled = false;
            //var productlist = db.Products.ToList();
            var product = new  { Product = productlist, TotalRecords = totalNumberOfRecords };

           return Json(new  { Products = productlist, RecordCount = totalNumberOfRecords }, JsonRequestBehavior.AllowGet); 
        }

我的 Angular 控制器代码是这样的

   function GetProducts() {

        var productResult = productService.getProduct($scope.currentPage, $scope.recordsPerPage);
        productResult.then(function (result) {
            console.log("d",data);
            if (result.data != '' || result.data != null) {

                if (result.data != null || result.data != '') {
                    $scope.Products = result.data;
                }
                else if (result.data = 0) {
                    $scope.message = "No Product Found"

                }
            }

        });
    };

Angular 服务代码是这样的

  this.getProduct = function (currentPage, recordsPerPage) {
        return $http.get('/Home/Get?currentPage=' + currentPage + '&recordsPerPage=' + recordsPerPage);
       // return $http.get('/Home/Get');
    };

我错过了一些东西,但我无法得到它。任何专家请在这方面帮助我。我整晚都在处理这个错误。我尝试了我阅读的所有 stackoverflow 解决方案,但对我没有任何帮助

这是我的模型

namespace StylesStore.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Product
    {
        public Product()
        {
            this.Carts = new HashSet<Cart>();
            this.OrdersDetails = new HashSet<OrdersDetail>();
        }

        public int ProductID { get; set; }
        public Nullable<int> SKU { get; set; }
        public Nullable<int> VendorProductID { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
        public Nullable<int> SupplierID { get; set; }
        public Nullable<int> CategoryID { get; set; }
        public Nullable<int> QuantityPerUnit { get; set; }
        public decimal UnitPrice { get; set; }
        public Nullable<int> MSRP { get; set; }
        public Nullable<int> AvailableSize { get; set; }
        public string AvailableColor { get; set; }
        public Nullable<int> Size { get; set; }
        public string Color { get; set; }
        public Nullable<int> Discount { get; set; }
        public Nullable<int> UnitWeight { get; set; }
        public Nullable<int> UnitsInStock { get; set; }
        public string UnitsInOrder { get; set; }
        public string Picture1 { get; set; }
        public string Picture2 { get; set; }
        public string Picture3 { get; set; }
        public string Picture4 { get; set; }
        public Nullable<decimal> ShippingCharges { get; set; }
        public string Note { get; set; }
        public Nullable<bool> InStock { get; set; }
        public Nullable<int> CatID { get; set; }
        public Nullable<decimal> wieght { get; set; }
        public Nullable<int> totalview { get; set; }
        public Nullable<int> Disable { get; set; }
        public Nullable<System.DateTime> EntryDate { get; set; }

        public virtual ICollection<Cart> Carts { get; set; }
        public virtual Category Category { get; set; }
        public virtual ICollection<OrdersDetail> OrdersDetails { get; set; }
        public virtual Supplier Supplier { get; set; }
    }
}

【问题讨论】:

  • Product 的模型是什么?它是否包含一个复杂对象的属性并包含一个属性Product(是循环引用)?
  • 我正在使用 db first 过程
  • 那不相关。你需要展示你的模型。
  • 我加了型号请看
  • 您有很多导航属性(CategorySupplierICollection&lt;Cart&gt;ICollection&lt;OrdersDetail&gt;),其中一些必须包含对 Product 的引用。您需要发送一组匿名对象,其中仅包含您在视图中需要的属性

标签: angularjs asp.net-mvc


【解决方案1】:

您不应该序列化您的实体对象。您的实体对象具有由 JsonSerializer 检测到的虚拟导航属性。 JsonSerializer 认为它们是该对象的嵌入属性,并尝试将它们序列化。这样下去。序列化程序发现自己试图序列化所有数据库。这就是您发生错误的原因。

您应该标记需要序列化的字段或使用 DTO 来序列化对象。

注意:您可以使用 AutoMapper 在 Entity 和 DTO 之间映射对象

【讨论】:

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