Reputation: 183
I am new in .net core programming.
I need to return a Json object within a Json object in .net core MVC. I want to return a list schedules and the infos of the teams that belongs on that schedules.
as of now I'm getting this output using this codes.
for SCHEDULES:
code in controller
[HttpGet]
public async Task<List<Schedules>> getAllScheds(){
return await _context.Schedules.ToListAsync();
}
output
[
{
"scheduleId": 43,
"teamId1": 1,
"teamId2": 3,
"scheduleDate": "2016-04-30T19:00:00",
"week": "1",
"stadiumId": 3,
"createdBy": null,
"createdDate": "2016-07-07T13:09:32.797"
},
{
"scheduleId": 44,
"teamId1": 7,
"teamId2": 6,
"scheduleDate": "2016-05-01T16:00:00",
"week": "1",
"stadiumId": 6,
"createdBy": null,
"createdDate": "2016-07-07T13:13:10.183"
},
{
"scheduleId": 45,
"teamId1": 2,
"teamId2": 4,
"scheduleDate": "2016-05-02T22:00:00",
"week": "1",
"stadiumId": 4,
"createdBy": null,
"createdDate": "2016-07-07T13:16:23.337"
}
]
for Team infos
code in controller
[HttpGet]
public async Task<List<Teams>> getAllTeamsInfo(){
return await _context.Teams.ToListAsync();
}
output
[
{
"teamId": 1,
"city": "Virginia",
"teamName": "Armada",
"sImage": "/images/teams-logo-small/virginia.png"
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58"
},
{
"teamId": 2,
"city": "Arkansas",
"teamName": "Attack",
"sImage": "/images/teams-logo-small/arkansas.png"
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58"
},
{
"teamId": 3,
"city": "Florida",
"teamName": "Fusion",
"sImage": "/images/teams-logo-small/florida.png"
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58",
"modifiedBy": null
}
]
Now I want to merge them and have this output.
[
{
"scheduleId": 43,
"teamId1": [{
"teamId": 1,
"city": "Virginia",
"teamName": "Armada",
"sImage": "/images/teams-logo-small/virginia.png"
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58"
}],
"teamId2": [{
"teamId": 3,
"city": "Florida",
"teamName": "Fusion",
"sImage": "/images/teams-logo-small/florida.png"
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58",
"modifiedBy": null
}],
"scheduleDate": "2016-04-30T19:00:00",
"week": "1",
"stadiumId": 3,
"createdBy": null,
"createdDate": "2016-07-07T13:09:32.797"
},
{
"scheduleId": 44,
"teamId1": [{
"teamId": 7,
"city": "Oklahoma",
"teamName": "Nation",
"sImage": "/images/teams-logo-small/oklahoma.png",
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58"
}],
"teamId2": [{
"teamId": 6,
"city": "Texas",
"teamName": "Independence",
"sImage": "/images/teams-logo-small/texas.png",
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58"
}],
"scheduleDate": "2016-05-01T16:00:00",
"week": "1",
"stadiumId": 6,
"createdBy": null,
"createdDate": "2016-07-07T13:13:10.183"
},
{
"scheduleId": 45,
"teamId1": [{
"teamId": 2,
"city": "Arkansas",
"teamName": "Attack",
"sImage": "/images/teams-logo-small/arkansas.png",
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58"
}],
"teamId2": [{
"teamId": 4,
"city": "Oregon",
"teamName": "Crash",
"sImage": "/images/teams-logo-small/oregon.png",
"createdBy": null,
"createdDate": "2016-06-22T10:03:35.58",
}],
"scheduleDate": "2016-05-02T22:00:00",
"week": "1",
"stadiumId": 4,
"createdBy": null,
"createdDate": "2016-07-07T13:16:23.337"
}
]
instead of the team ID, I want to return the whole Team info.
Thank you very much.
Upvotes: 1
Views: 57
Reputation: 2837
If you don't have the Team navigation properties in your Schedules model you have to add them first:
public class Schedules {
[ForeignKey("Team1Id")]
public Team Team1 {get;set;}
[ForeignKey("Team2Id")]
public Team Team2 {get;set;}
}
Then you can use the Include(s=>s.Team1).ThenInclude(s=>s.Team2) in your query.
An other way is to create a new model that represent the values you want. This will be the result of your controller method. For example:
public class ScheduleDTO:Schedule {
public Team Team1 {get;set;}
public Team Team2 {get;set;}
}
In the request you select this new model for each Schedule:
[HttpGet]
public async Task<List<ScheduleDTO>> getAllScheds(){
return await _context.Schedules.Select(s=>new ScheduleDTO() {
scheduleId: s.Id,
teamId1: s.Team1Id,
teamId2: s.Team2Id,
scheduleDate: "2016-04-30T19:00:00",
week: "1",
stadiumId: 3,
createdBy: null,
createdDate: "2016-07-07T13:09:32.797",
Team1:s.Team1,
Team2:_context.Teams.FirstOrDefault(x=>x.Id==s.Team1Id) //not recommended
});
}
Upvotes: 1