I have 2 questions:
What is the difference between JSONResult and ActionResult?
When to use JSONResult in MVC?
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 .
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 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With