Click here to Skip to main content
16,020,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 tables in hierarchical structure namely album, artist and genre.
album table derived from genre and artist.

artist properties

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


genre properties:
C#
[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:

SQL
[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; }



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

in controller
C#
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:

JavaScript
 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.
Posted
Updated 12-Aug-21 5:25am
Comments
Mehdi Gholam 13-Oct-14 4:20am    
If you are passing it to the web browser there is very little you can do, other than avoid circular references in your data structure.

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);
 
Share this answer
 
v2
my solution depends on using EF6 & LINQ

simply

var x = db.className.select(s=>{

prop1=s.prop1,
prop2=s.prop2
}).tolist();

return json(x);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900