Learn different types of Action Results in MVC

1
39536
MVC

Hi all, today we are going to study about different types of action results in MVC. In MVC controller may have one or more actions and can return different types of results like if you want to return Json, JavaScript, empty result, return view, etc. ActionResults is the base class of all result types. ActionResults is an abstract class.  As ActionResults is the base class for all types of action results this means return type of ActionResults can be any of the below-listed result types. In this topic, we will learn how to use ViewResult, PartialViewResult, RedirectResult, and JsonResult.

Types of action results

ActionResult Helper Method Description
ViewResult View ViewResult Renders a view as a web page.
PartialViewResult PartialView As the name describe PartialViewResult renders the partial view.
RedirectResult Redirect When you want to redirect to another action method we will use RedirectResult
RedirectToRouteResult RedirectToRoute Redirect to another action method
ContentResult Content Returns a user-defined content type
JsonResult Json When you want to return a serialized JSON object
JavaScriptResult JavaScript Returns a script that can be executed on the client
FileResult File Returns a binary output to write to the response
EmptyResult (None) returns a null result

Now let’s start with the example

Open visual studio and create new MVC application.

ViewResult:

Now create new view in home folder inside View folder and rename it to ActionDemo.

Now open home controller inside controller folder and write below code in it and run the program.

Output:

ViewResult represents a class that is used to render a view by using an IView instance that is returned by an IViewEngine object. View() creates an object that renders a view to the response.


Learn HTML5 Programming For Beginners

PartialViewResult:

Open Shared folder inside view folder and add new view and rename it to PartialDemo.

Open the PartialDemo view and add the below code in it

Now open the ActionDemo view and write the below code in it.

Now open the home controller and write the below code in it

Partial

Html.Partial method return the string output, you can put output into the variable and manipulate it if required.

RenderPartial

 Html.Partial method will directly written to the http response stream

RenderAction

If you want to call action method of partial view then you can use Html.RenderAction method.

OutPut:
8

RedirectResult

Redirect result is used to redirect to one action to another action. Now open home controller and add below code in it.

Now open ActionDemo view and add below code in it.


10

First parameter in Html.ActionLink in Text of link, second parameter is Action Name and third parameter is Controller Name.

Now run the application. Click on RedirectLink it will get redirected to Index View.

Output
11

JsonResult

When you want to return data in Jason format we use JsonResult. Now let’s start with the demo. Open your Home Controller and write the below code in it and then run the application

Output
13

Conclusion

I hope you have enjoyed this tutorial and it helped you in understanding the topic in a more detailed way. You can also check out our other articles pertaining to MVC such as how to create a Spring MVC application and about Filters and its types in MVC.

Also, you can learn more tricks related to MVC such as create a simple MVC application in asp.net.

To learn more about ASP.NET, try out the “Projects in ASP.NET Core 2.0” course. The product comes with over 6 hours of video that covers 7 sections. They include topics such as ASP.NET Core 2.0 overview, date API application, shopping cart, creating other applications and much more.

We would love to know your feedback from you regarding this tutorial. Let us know in the comments below.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here