Return multiple objects with JSON result

I am wondering if it is possible to return multiple objects with a JSON result in MVC. At the moment, I have no problem returning a single object.

public ActionResult AddToBasket(int quantity, int productdetailid) { // more code here return Json ( new { Name = p.Product.Name, Price = p.Price}); } 

This returns a single anonymous object in my ajax call. What I want to do is return a few names and prices to populate the table in my view.

So I want to refresh (refresh) the cookie every time a user adds an item to their cart and updates the cart, which is an html table.

Thanks in advance.

+7
source share
2 answers

Just return some enumerables if you want an array:

 return Json ( Enumerable.Range(0, 10).Select(i => new { Name = "N" + i, Price = i }); 
+4
source

Just return an array of objects, for example:

 [ { Name: 'foo', Price: 123 } , { Name: 'bar', Price: 456 } , { Name: 'baz', Price: 789 } ] 
+6
source

All Articles