【问题标题】:Self referencing loop detected for property Error in MVC为 MVC 中的属性错误检测到自引用循环
【发布时间】:2014-08-27 07:44:24
【问题描述】:

我在淘汰赛中使用 MVVM。当我尝试在 mvc 中序列化对象时,出现以下错误。任何人都可以帮我解决这个问题。

错误行:return Json(JsonConvert.SerializeObject(resultwrapper), JsonRequestBehavior.AllowGet);

错误 检测到类型为“DCL.HotelDetails”的属性“HotelDetails”的自引用循环。路径“HotelDetails[3].HotelImages[0]”。

我的模特

 public class HotelDetails : Entity
    {

        public HotelDetails()
    {
        this.HotelImages = new List<HotelImages>();
    } 
        [Key]
        public virtual Guid Id { get; set; }

        public virtual Guid HotelChainId { get; set; }

        public virtual int OldHotel { get; set; }

        public virtual string StarRating { get; set; }

        public virtual string PostalCode { get; set; }

        public virtual string Longtitude { get; set; }

        public virtual string Latitude { get; set; }

        public virtual string DestinationID { get; set; }

        public virtual string HotelLocation { get; set; }

        public virtual string PhoneNumber { get; set; }

        public virtual string Address { get; set; }

        public virtual string HotelArea { get; set; }

        public virtual string HotelType { get; set; }

        public virtual string HotelTheme { get; set; }


        //public virtual Destination Destination { get; set; }

        public virtual List<HotelImages> HotelImages { get; set; }

    }

我的 C# 代码

 IEnumerable<IGrouping<string, DCL.JsonMatrixModel.availableHotels>> quer =
      from ff in ddd
      from ss in ff.availableHotels
      group ss by ss.hotelCode;


            List<HotelDetails> lsthoteldetails = new List<HotelDetails>();
            List<HotelImages> lsthotelimages = new List<HotelImages>();


            HotelDetails _hoteldetails;


            foreach (var y in quer)
            {
                _hoteldetails = new HotelDetails();


                string ss = y.Key;
                _hoteldetails = db.HotelDetails.Where(x => x.Code == ss).FirstOrDefault();
                if (_hoteldetails != null)
                {
                    lsthotelimages = db.HotelImages.Where(x => x.HotelDetailsId == _hoteldetails.Id).ToList();
                    _hoteldetails.HotelImages = lsthotelimages;
                    lsthoteldetails.Add(_hoteldetails);
                }





            }
           // resultwrapper.key = "kdkdk";
            resultwrapper.LoadData();

            resultwrapper.HotelDetails = lsthoteldetails;



            return Json(JsonConvert.SerializeObject(resultwrapper), JsonRequestBehavior.AllowGet);

【问题讨论】:

  • HotelImages 是否可以引用HotelDetails
  • 可以,可以参考HotelDetails
  • OK,导致循环引用。让我检查正确的设置以使其正常工作,我会发布答案

标签: c# asp.net-mvc json linq


【解决方案1】:

如果HotelImages 有对HotelDetails 的引用,那么您很可能得到一个循环引用。您可以通过设置PreserveReferencesHandling = PreserveReferencesHandling.Objects 来防止这种情况。例如

string json = JsonConvert.SerializeObject(resultwrapper, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });

Refer documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2021-12-26
    • 2017-02-13
    相关资源
    最近更新 更多