如何删除错误“在序列化类型为“对象”的对象时检测到循环引用。 [英] how to remove error " A circular reference was detected while serializing an object of type "

查看:117
本文介绍了如何删除错误“在序列化类型为“对象”的对象时检测到循环引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个分层结构的表,即专辑,艺术家和流派。

来自流派和艺术家的专辑表。



< b>艺术家属性



I have 3 tables in hierarchical structure namely album, artist and genre.
album table derived from genre and artist.

artist properties

[Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int ArtistId { get; set; }
      public string Name { get; set; }
      public List<Album> Albums { get; set; }





流派属性:



genre properties:

[Key]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public int GenreId { get; set; }
       public string Name { get; set; }
       public string Description { get; set; }
      public List<Album> Albums { get; set; }





专辑属性:





album properties:

[Key]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public int AlbumId { get; set; }
          [Required(ErrorMessage = "pls select Genre")]

       [ForeignKey("Genre")]
       public int GenreId { get; set; }

       [ForeignKey("Artist")]
       public int ArtistId { get; set; }

       [Required(ErrorMessage = "Title is required")]
       public string Title { get; set; }
       [Required(ErrorMessage = "Price is required")]
       [Range(0.01, 100.00,ErrorMessage = "Price must be between 0.01 and 100.00")]
       public decimal Price { get; set; }
       public string AlbumUrl { get; set; }
       public string Comments { get; set; }
       public   Genre Genre { get; set; }
       public  Artists Artist { get; set; }







需要从相册中检索数据,其中相应的流派名称对应于流派ID和艺术家姓名对应于艺术家姓名。



控制器




need to retrieve the data from album with respective Genre name corresponding to genre id and artist name corresponding to artist name.

in controller

public ActionResult GetAlbums(int rowsPerPage, int pageNo)
       {
           GridDataResult gridDataResult = new GridDataResult();
           var albums = logic.GetAlbum();
           gridDataResult.TotalRowCount = albums.Count;
           int totalPages = 1;
           if (rowsPerPage > 0)
           {
               totalPages = gridDataResult.TotalRowCount / rowsPerPage;
               if (gridDataResult.TotalRowCount % rowsPerPage != 0)
                   totalPages += 1;
           }
           gridDataResult.TotalPageCount = totalPages;

           gridDataResult.GridData = albums.Skip((pageNo - 1) * rowsPerPage).Take(rowsPerPage).ToList();

        // var data =JsonConvert.SerializeObject(gridDataResult, Formatting.None);

         //return Json(data, JsonRequestBehavior.AllowGet);
         return Json(gridDataResult, JsonRequestBehavior.AllowGet);
       }



直到这里数据即将到来但在视图页面中我使用以下代码:




till here data is coming but in view page i used following code:

 function loadAlbums(self)
        {
            $.ajax({
                url: "/MuzzicStore/GetAlbums?rowsPerPage=" + self.rowsPerPage() + "&pageNo=" + self.page(),
                        type: "get",
                        success: function (data) {
                            alert('success');
                            //self.Albums.removeAll();
                            //var serverData = ko.utils.parseJson(data);
                            //self.totalPages(serverData.TotalPageCount);
                            //self.Albums.removeAll();
                            //self.Albums(serverData.GridData);
                        },
                        error: function () {
                           alert("Error");

                        }
            });
          
        }

its giving an error

A circular reference was detected while serializing an object of type 'MuzzicMantra.Data.Domain.Album'.

tried lot of thongs but couldnt resolve.

help will be appreciable.

推荐答案

.ajax({
url: / MuzzicStore / GetAlbums?rowsPerPage = + self.rowsPerPage()+ & pageNo = + self.page(),
type: 获取
成功:功能(数据){
alert (' success');
// self.Albums.removeAll();
// var serverData = ko.utils.parseJson(data);
// self.totalPages(serverData.TotalPageCount);
// self.Albums.removeAll();
// self.Albums(serverData.GridData);
},
错误: function (){
alert ( 错误);

}
});

}

给出错误

检测到循环引用 序列化类型为' MuzzicMantra.Data.Domain.Album'的对象。

试了很多丁字裤,但无法解决。

帮助将是可观的。
.ajax({ url: "/MuzzicStore/GetAlbums?rowsPerPage=" + self.rowsPerPage() + "&pageNo=" + self.page(), type: "get", success: function (data) { alert('success'); //self.Albums.removeAll(); //var serverData = ko.utils.parseJson(data); //self.totalPages(serverData.TotalPageCount); //self.Albums.removeAll(); //self.Albums(serverData.GridData); }, error: function () { alert("Error"); } }); } its giving an error A circular reference was detected while serializing an object of type 'MuzzicMantra.Data.Domain.Album'. tried lot of thongs but couldnt resolve. help will be appreciable.


它对我有用



需要解决三个参考资料:



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);


这篇关于如何删除错误“在序列化类型为“对象”的对象时检测到循环引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆