0

How can I get multiple json objects from single MVC controller? In below example if I only want to return fishes (pond) I would do:

return Json(pond, JsonRequestBehavior.AllowGet)

but how to return both fishes and trees in single json?

   public ActionResult PondAndForestData()
    {
        List<Fish> pond = context.getAllFishes();
        List<Tree> forest = context.getAllTrees();
        // return both as one combined Json?
    }
1
  • You can return array in one JSON object Commented Jan 26, 2016 at 16:20

2 Answers 2

1

Create a new anonymous object with 2 properties

public ActionResult PondAndForestData()
{
    List<Fish> pond = context.getAllFishes();
    List<Tree> forest = context.getAllTrees();
    return Json(new { Ponds= pond, Trees= forest},JsonRequestBehaviour.Allow.GET);
}

In your client side whereever you are calling this action method, you should access Ponds / Trees property as needed.Both of them will be an array.

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

Comments

0

Create a new Class containing the following as member variables. Then pass an instance of that class as JSON.

List<Fish> pond = context.getAllFishes();
List<Tree> forest = context.getAllTrees();

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.