I have this bit of code to send a List of Route
objects to my View (ASP.Net MVC):
public ActionResult getRouteFromPart(int partId)
{
List<Route> routes = _routeService.GetRouteByPartType(partId);
if (routes == null)
{
return this.AdvancedJsonResult(null, JsonRequestBehavior.AllowGet);
}
return this.AdvancedJsonResult(new
{
Routes = routes
}, JsonRequestBehavior.AllowGet);
}
But I'm getting an exception here in my AdvancedJsonResult
class:
if (Data != null)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
string result = JsonConvert.SerializeObject(this.Data, this.Formatting, settings);
response.Write(result);
}
I've tried the "ReferenceLoopHanding.Ignore" trick which silences the exception, but the list still doesn't get passed to the view.
The code works when I change routes
to a single object instead of a list, so I think the code just doesn't like working with a list.
I'm new to this project so I'm not sure how to fix this and make it happy with using a List...
Edit: Here's the full Exception message, which happens on the string result = JsonConvert...
line.
Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.PartNumber_B135A5D16403B760C3591872ED4C98A25643FD10B51246A690C2F2D977973452'. Path 'routes[0].incomingLots[0].partNumber.partType.partNumbers'.
Route
. The code you show seems fine. – rene