Json and Circular Reference Exception

前端 未结 9 1596
挽巷
挽巷 2020-11-27 03:17

I have an object which has a circular reference to another object. Given the relationship between these objects this is the right design.

To Illustrate

9条回答
  •  粉色の甜心
    2020-11-27 04:02

    What I have done is a bit radical, but I don't need the property, which makes the nasty circular-reference-causing error, so I have set it to null before serializing.

    SessionTickets result = GetTicketsSession();
    foreach(var r in result.Tickets)
    {
        r.TicketTypes = null; //those two were creating the problem
        r.SelectedTicketType = null;
    }
    return Json(result);
    

    If you really need your properties, you can create a viewmodel which does not hold circular references, but maybe keeps some Id of the important element, that you could use later for restoring the original value.

提交回复
热议问题