0

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... " }]

2
  • 2
    on a side note, I think you're miss-using JSON here. By definition JSON is to allow exchange of data between server and client in a lightweight way in an object notation. Normally you wouldn't put HTML into your C# entity would you? Why going with JSON here? Commented Dec 20, 2011 at 8:33
  • @torm ya i realized that....Now, i am using the child action and passing them directly. Thanks for pointing and explaining out Commented Dec 20, 2011 at 9:53

1 Answer 1

2

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.

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

2 Comments

I have an ActionResult method which returns Json("string"). This one simple passes string with no braces or brackets, why so ??
Because that's how a string is serialized into json. Use an anonymous object if you want to get an object as the output: Json(new { "aaa"});

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.