1

I'm probably wording my searches wrong but I've yet to find anything related to returning "more" json from a controller than just the controller / model I'm working in. What I've read makes me want to believe that I have a relationship or mapping issue on my database but again, I'm not sure where to start.

Currently my controller is only returning:

{
  "Id": 43,
  "CreatedDate": "2015-02-09T00:00:00",
  "CreatedBy": "username",
  "ClosedDate": "2015-02-09T00:00:00",
  "ClosedBy": "username",
  "Revision": "1"
}

I'm trying to return something that looks like:

{
    "JobDetails": {
        "Tasks": [{
            "Id": 1,
            "TaskName": "string",
            "TaskDescription": "string",
            "TaskOrder": 1,
            "TimeCompleted": "DateTime",
            "CompletedBy": "string",
            "Notes": [{
                "Note": "This is note 1.",
                "CreatedDate": "DateTime",
                "CreatedBy": "string"
            }, {
                "Note": "This is note 2.",
                "CreatedDate": "DateTime",
                "CreatedBy": "string"
            }]
        }]
    }
}

The content comes from my "JobsController" after a POST which you can see populates the other fields.

// POST: api/Jobs
[Route("api/Jobs/PostJob/", Name="PostJobRoute")]
[ResponseType(typeof(Job))]
public IHttpActionResult PostJob(Job job)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    //Run query to get a list of all tasks that have the group ID passed
    IQueryable<Task> TasksQuery =
        from c in db.Tasks
        select c;

    //Execute and loop results...
    IQueryable<Task> TasksByGroupId = TasksQuery.Where(c => c.TaskGroupId == 1);

    foreach (var task in TasksByGroupId)
    {
        db.JobDetails.Add(new JobDetail()
        {
            //Generate a new job detail for this job
            JobId = job.Id,
            TimeCompleted = DateTime.Now,
            CompletedBy = "ocodyc",
            TaskId = task.Id,
            TaskIdOrder = task.TaskOrder
        });
    }

    //TODO: Return a list of the task + notes

    db.Jobs.Add(job);
    db.SaveChanges();

    return CreatedAtRoute("PostJobRoute", new { id = job.Id }, job);
}

Does this seem to be a mapping issue? I've tried adding a navigation property to the "jobs" to "jobdetails" but that didn't work.

4
  • 3
    You should create a class for how you want to represent your data and return that rather than just returning the db object or attempting to restructure your db. If you are unable to get that extra data because you can't relate to it, then yes you have a mapping problem. Commented Feb 9, 2016 at 20:44
  • That would make sense. I'll work on it from that direction. Commented Feb 9, 2016 at 20:45
  • 1
    Create a view model that's identical to your desired output. Then map your domain model into the view model. You can handcraft it or use AutoMapper for the mapping. Commented Feb 9, 2016 at 21:24
  • Your on the right track with your TODO comment Commented Feb 9, 2016 at 21:26

1 Answer 1

1

You're return the object you passed into the action, that's why. Don't return job, do something like this:

var jobDetailList = new List<JobDetail>();
foreach (var task in TasksByGroupId)
{
    jobDetailList.Add(new JobDetail()
    {
        //Generate a new job detail for this job
        JobId = job.Id,
        TimeCompleted = DateTime.Now,
        CompletedBy = "ocodyc",
        TaskId = task.Id,
        TaskIdOrder = task.TaskOrder
    });
}

//TODO: Return a list of the task + notes

db.Jobs.AddRange(jobDetailList);
db.SaveChanges();

return CreatedAtRoute("PostJobRoute", new { id = job.Id }, jobDetailList.AsEnumerable());

aw04 is right though. You may want to implement a DTO pattern and project you're domain into that

Sign up to request clarification or add additional context in comments.

1 Comment

Adding that list gives me an error saying I can't convert from List to Enumerable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.