4

So I have this JsonResult:

[HttpPost]
public JsonResult getJsonInvoicesClient(int id_client)
{
    var a = db.invoices
        .Where(x => x.id_client == id_client)
        .ToList();
    return Json(a);
}

and in jquery I receive a list of invoice objects. Now I need to add the client data to the json, so I get it from the db with something like:

    var c = db.clients.FirstOrDefault(p => p.id == id_client);

But how can I add this client object to the json... so that in jquery I receive a list or similar with just 2 items: the client object, and the list of invoice objects?

1 Answer 1

3

Use anonymous type:

var invoices = db.invoices
    .Where(x => x.id_client == id_client)
    .ToList();
var clinet = db.clients.FirstOrDefault(p => p.id == id_client);
return Json(new
{
    clinet,
    invoices
});
Sign up to request clarification or add additional context in comments.

Comments

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.