Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentResult vs JsonResult with ajax

I recently found some samples of code with Asp.Net Mvc2 that makes some ajax calls to actions in controller that returns ContentResult.

I experienced some problems while trying to convert these samples in Mvc3 without changing the return type of actions to JsonResult.

Can anyone explain to me what is the difference between ContentResult and JsonResult in the context of ajax calls and in which cases can I use one or the other in the context of ajax calls? I found a good post here that explains in detail each type of result but it's not enough to answer my question.

Is there a known issues with ContentResult and ajax calls in Mvc3?

Thank you.

like image 684
Samuel Avatar asked Apr 02 '12 16:04

Samuel


1 Answers

If the result your JavaScript code gets back is a piece of content such as HTML, you should return a ContentResult or a PartialViewResult (if the HTML is formatted using a partial view).

If the result is an object (especially one you then want to manipulate), then it should be a JsonResult.

Here are some good examples of working with JSON: http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx

Good luck!

like image 97
Roy Dictus Avatar answered Oct 14 '22 03:10

Roy Dictus