Matthew Hood
Matthew Hood

Reputation: 978

Recommended approach for linq to sql serialization for simple json and mvc

I have cascading drop down lists in a .net mvc view and am using JQuery onchange to pull the next drop down list using a json response. The problem comes in where iI get a circular reference when trying to serialize the linq to sql response.

I understand why it is happening, its because I have the buyer -> contracts and contract -> buyer relationships which will be circular when serialized.

I am wondering what the recommended solution is. Should I create a new json specific object and map the fields I need for the response?

Is there a way of ignoring the relationships when serializing and just returning this table (i dont need any relationships as its just a drop down list).

Upvotes: 0

Views: 1046

Answers (2)

Jeremy
Jeremy

Reputation: 3548

Make the child relationship 'interal' rather than public in the DBML.

See Rick Strahl's post for more information. http://www.west-wind.com/weblog/posts/2007/Sep/02/LINQ-to-SQL-and-Serialization

Upvotes: 0

Reza
Reza

Reputation: 181

I hit this exact same problem recently and it appears there is no real elegant solution to this short of using something like Json.NET (which I've not tried myself but is supposed to not throw the circular reference error).

The solution I ended up using (for my tiny project) was to set the referenced entity to null prior to serialisation. This could get old very quickly for larger projects which I where Json.NET would come in handy I think.

Upvotes: 1

Related Questions