Reputation: 7818
this is related to a post I made yesterday, but havent been able to resolve: ASP.Net Web API showing correctly in VS but giving HTTP500
I think I need to simplify what I'm trying to do, and work up from there.
Can anyone please point me to an example of using the asp.net Web API (I'm using VS 2012 Express RC), to return JSON from a parent/child model?
eg: (pseudo Json):
Parent: Mark
..Child: Tom
..Child: Adam
..Child: Becki
Parent: Terry
..Child: Sophie
..Child: robert
I can get it to return data from one table, but not from a linked table.
Thanks for any help,
Mark
Upvotes: 0
Views: 3317
Reputation: 2824
After looking at your original post my guess is that you have circular references in your objects. This post makes reference to using Json.Net which will give you more control over what is being returned to the client.
Your other option is to remove the foreign key reference tblCustomerBooking
from the tblRental
object (see below).
This may allow you to return the JSON objects and test that circular references are the issue.
[ForeignKey("customer_id")]
public virtual tblCustomerBooking tblCustomerBooking { get; set; }
I do suggest using Json.NET if you're planning on returning your Domain (i.e. Entity Objects) as this will avoid all circular references, and allow you keep your two-way object relationships.
My personal preference is to using DTO's and map your Domain objects to these DTO's, allowing you to have more control over what the client sees (as seeing the 'tbl' prefix in a object name isn't good practice)
Upvotes: 0
Reputation: 63
There are two ways to go away with this.
Create a new template class, loop through the list fetched using the EF and assign the values to the properties defined in the template class. This would give you the accurate results from one to multiple tables if any. Finally return the list to the json call.
While fetching the list from the EF, create a new anonymous type and select your desired columns. For this your webmethod would have the return type as IEnumerable
Cheers!
Upvotes: 1