We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Circular reference errors from UrlDatasource Json serializer

Thread ID:

Created:

Updated:

Platform:

Replies:

140702 Nov 2,2018 12:48 AM UTC Nov 7,2018 04:56 AM UTC ASP.NET MVC - EJ 2 5
loading
Tags: Grid
Vanderlei
Asked On November 2, 2018 12:48 AM UTC

Hi guys,

I am having circular reference problems when trying to get table data from related tables. There is a M:N relationship between aspnetusers and aspnetroles. Join table apnetuserroles is not mapped by EF6, so I am using this code to get data, and results are ok:
IEnumerable DataSource = db.aspnetusers.Where(c => c.aspnetroles.Any()).ToList<aspnetusers>();

But some circular reference errors ocurrs when trying to serialize data for returning json results:
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);

1 - Is there an option to avoid this errors?

2 - I've found some workarounds for this problem. So I tried the following using Newtonsoft json serializer:
var list = JsonConvert.SerializeObject(DataSource, Formatting.None, new JsonSerializerSettings()
            {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            });
This code serializes data without errors. Is it possible to send its results to grid?

Thank you! 

Madhu Sudhanan P [Syncfusion]
Replied On November 2, 2018 09:41 AM UTC

Hi Vanderlei, 

Thanks for contacting Syncfusion support. 

Query : Circular reference errors from UrlDatasource Json serializer 
 
When serializing multiple entities there might be changes for circular reference exception and hence we suggest you to use the ViewModel instead of ReferenceLoopingHandling to resolve the issue. 

 
Kindly refer to the below code example for more information. In the below code example we have created viewModel class and returned the ViewModel list instead of the original entity. 
 
 
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }).Columns(col => 
       { 
           col.Field("AddressType").HeaderText("AddressType").Width("120").Add(); 
           col.Field("AddressLine1").HeaderText("AddressLine1").Width("120").Add(); 
           col.Field("Title").HeaderText("Title").Width("120").Add(); 
 
       }).AllowPaging().AllowFiltering().Render() 

// you can bind the data that you want to show in Grid    
public class CustomerAddressVM  
    { 
        public string AddressType { get; set; } 
 
        public string AddressLine1 { get; set; } 
 
        public string Title { get; set; } 
    } 

using EntityExampleWithRelationships.ViewModels; 
 
. . . . . 
public ActionResult UrlDatasource(DataManagerRequest dm) { 
 
//Original entity collection 
var customerAddresses = db.CustomerAddresses.Include(c => c.Address).Include(c => c.Customer); 
 
//Viewmodel entity collection 
            List<CustomerAddressVM> customer = new List<CustomerAddressVM>(); 
            foreach (var item in customerAddresses) 
 
            { 
 
                CustomerAddressVM objcvm = new CustomerAddressVM(); // ViewModel 
 
                objcvm.Title = item.Customer.Title; 
 
                objcvm.AddressLine1 = item.Address.AddressLine1; 
 
                objcvm.AddressType = item.AddressType; 
 
                customer.Add(objcvm); 
 
            } 
return dm.RequiresCounts ? Json(new { result = customer.ToList(), count = count }) : Json(customer.ToList()); 


Regards, 
Madhu Sudhanan P 


Vanderlei
Replied On November 2, 2018 02:22 PM UTC

Hi Madhu, thanks for your reply.

Will I be able to CRUD by using that approach? I mean, creating a viewmodel for aspnetusers and aspnetroles and saving its changes, will PKs in join table aspnetuserroles (M:N relationship table not mappped in EF6) be updated/inserted?

Many thanks!

Madhu Sudhanan P [Syncfusion]
Replied On November 5, 2018 12:53 PM UTC

Hi Vanderlei, 

Query : Will I be able to CRUD by using that approach?  
 
Yes, you can able to CRUD operation using the below approach.  In the below code example, We have got the updated record in CustomerAddressVM object and remapped to the original entity which then can be used to update to the db. 

         
        public ActionResult Update(CustomerAddressVM value) 
        { 
            var customer = db.CustomerAddresses.Where(c => c.CustomerID === customerModel.CustomerID); 
 
            customer.Customer.Title = customerModel.Title; 
 
            customer.Address.AddressLine1 = customerModel.AddressLine1; 
 
            customer.AddressType = customerModel.AddressType; 
             
            // Use the customer class to update the records 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
 

Regards, 
Madhu Sudhanan P 


Vanderlei
Replied On November 5, 2018 01:11 PM UTC

Ok Madhu,

Thanks!

Madhu Sudhanan P [Syncfusion]
Replied On November 7, 2018 04:56 AM UTC

Hi Vanderlei, 
We are glad that your requirement has been achieved. 
Regards, 
Madhu Sudhanan P 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;