Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A circular reference was detected while serializing an object of type 'System.Reflection

I have an asp.net MVC 3 controller action method like this:

public JsonResult GetRecordingRates(int Id)
{            
    List<DefaultRateChart> defaultRateCharts = new List<DefaultRateChart>();
    using (IDefaultRateChartManager defaultRateChartManager = new ManagerFactory().GetDefaultRateChartManager()) {
       defaultRateCharts = defaultRateChartManager.GetAll().Where(rc => rc.Currency.Id == Id && (!rc.NumberPrefix.StartsWith("#") || rc.NumberPrefix.StartsWith("Default")) && rc.AccountCredit == "Credit").ToList();
    }
    return Json(defaultRateCharts);
}

I want to send this list to jquery ajax success method but I am getting 500 Internal Server Error

my ajax call is like this:

$.ajax({
type: "POST",
dataType: "json",
url: "/Home/GetRecordingRates",
data: {
    Id: $("#hdCurrencyId").val()                 
},
success: function (data) {
        alert(data);
   }
}); 

In firebug XHR under response tab, it says:

A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.

[EDIT]

I changed the action method to this:

public JsonResult GetRecordingRates(int Id)
{
    List<DefaultRateChart> defaultRateCharts = new List<DefaultRateChart>();
    using (IDefaultRateChartManager defaultRateChartManager = new ManagerFactory().GetDefaultRateChartManager())
    {
        defaultRateCharts = defaultRateChartManager.GetAll().Where(rc => rc.Currency.Id == Id && (!rc.NumberPrefix.StartsWith("#") || rc.NumberPrefix.StartsWith("Default")) && rc.AccountCredit == "Credit").ToList();
    }

    return this.Json(
        new
        {
            Result = (from obj in defaultRateCharts select new { Id = obj.Id, DialPrefix = obj.NumberPrefix, Rate = obj.PurchaseRates })
        }
        , JsonRequestBehavior.AllowGet
     );
}

and I dont get that error now but how can I parse the collection in ajax success. I changed success to following but it is not adding rows in table.

success: function (data) {
            var row = $('<tr>');
            for(var i = 0; i < data.length; i++) {
                row.append($('<td>').html(data[i]));
            }   

            $('#results').append(row);                                      
            jQuery('#RecordingRates').dialog({ closeOnEscape: false });
            $(".ui-dialog-titlebar").hide();
            $("#RecordingRates").dialog({ dialogClass: 'transparent' });
            $('#RecordingRates').dialog('open');
           }
    }); 

In Firebug Net => XHR=> Json, It shows following JSON:

[Object { Id=

1

,  DialPrefix=

"1"

,  Rate=

2.6

}, Object { Id=

3

,  DialPrefix=

"2"

,  Rate=

2.6

}, Object { Id=

5

,  DialPrefix=

"7"

,  Rate=

3.5

}, 3 more...]


0
    Object { Id=

1

, DialPrefix=

"1"

, Rate=

2.6

}

1
    Object { Id=

3

, DialPrefix=

"2"

, Rate=

2.6

}

2
    Object { Id=

5

, DialPrefix=

"7"

, Rate=

3.5

}

3
    Object { Id=

7

, DialPrefix=

"8"

, Rate=

6

}

4
    Object { Id=

9

, DialPrefix=

"Default"

, Rate=

5

}

5
    Object { Id=

15

, DialPrefix=

"Subscription"

, Rate=

15

}
like image 433
DotnetSparrow Avatar asked Apr 29 '13 13:04

DotnetSparrow


1 Answers

This happens when two or more objects in your JSON points to each other, the serializer cannot reflect those into JSON. You should use the Newtonsoft serializer (It is explained here how to do it: Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?)

There are several things you can do. The first was one is let the configuration manager know to ignore or preserve those references like this:

        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;

or

you could annotate the properties you don't want to be serialized in the objects (So a circular reference won't exist). With the annotation [JsonIgnore] like this:

public class Order
{
    [JsonIgnore]
    public virtual ICollection<OrderLines> OrderLines{ get; set; } 
}
like image 78
Nick N. Avatar answered Oct 15 '22 10:10

Nick N.