Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

Passing Json from action method in Asp.net MVC

I am fetching the last five added products like following :-

var products = pe.Products.OrderByDescending(p => p.Id).Take(5);

Now, I want to pass the above data as a Json in the following format :-

[{"content": "<div class='slide_inner'><a class=' continues... ","content_button": "<div class='thumb'><img src=' coninues... " }]

Upvotes: 0

Views: 280

Answers (1)

St&#233;phane Bebrone
St&#233;phane Bebrone

Reputation: 2763

From your Controller, you can return a JsonResult as the result of your action:

return Json(products)

Behind the scene, your Productcollection will be serialized in a json string by the JavaScriptSerializer.

Upvotes: 2

Related Questions