Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple objects with a 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 to return 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 wanna do is return multiple Names and Prices to fill a table in my view.

So basicly i wanna update(renew) the cookie every time the user adds a item to his basket and update the basket which is a html table.

Thanks in advance.

like image 215
Wartodust Avatar asked Apr 04 '12 20:04

Wartodust


2 Answers

Simply return an array of objects, e.g:

[ { Name: 'foo', Price: 123 }
, { Name: 'bar', Price: 456 }
, { Name: 'baz', Price: 789 } ]
like image 180
buley Avatar answered Oct 16 '22 01:10

buley


Just return some enumerable if you want an array:

return Json ( Enumerable.Range(0, 10).Select(i => new { Name = "N" + i, Price = i });
like image 41
Alexei Levenkov Avatar answered Oct 16 '22 02:10

Alexei Levenkov