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
}