Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionresult vs JSONresult

I have 2 questions:

  1. What is the difference between JSONResult and ActionResult?

  2. When to use JSONResult in MVC?

like image 304
Sameer More Avatar asked Mar 06 '13 15:03

Sameer More


People also ask

What is a JsonResult?

What is JsonResult ? JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format). In this article we will learn about JsonResult by taking scenario to bind view using the JSON Data .

What is the difference between ActionResult and ViewResult?

ActionResult is an abstract class, and it's base class for ViewResult class. In MVC framework, it uses ActionResult class to reference the object your action method returns. And invokes ExecuteResult method on it. And ViewResult is an implementation for this abstract class.

What does ActionResult mean?

What is an ActionResult? An ActionResult is a return type of a controller method, also called an action method, and serves as the base class for *Result classes. Action methods return models to views, file streams, redirect to other controllers, or whatever is necessary for the task at hand.

What is ActionResult used for?

The ActionResult method works as a return type of any controller method in the MVC. It acts as the base class of Result classes. It is used to return the models to the Views, file streams, and also redirect to the controllers. It is the responsibility of the Controller that connects the component.


2 Answers

ActionResult is an abstract class that an action can return.

The helper methods in Controller (eg, Json(), Content(), View(), ...) return different concrete classes that inherit ActionResult, including JsonResult.

You should declare your action methods as returning ActionResult, so that they have the freedom to return any concrete result class.

like image 120
SLaks Avatar answered Sep 17 '22 18:09

SLaks


Use JsonResult when you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client).

Use ActionResult if you want to return a view, redirect etc to be handled by a browser.

like image 33
Trevor Pilley Avatar answered Sep 17 '22 18:09

Trevor Pilley