Learn different types of Action Results in MVC

1
35166
MVC

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 type of result like if you want to return Json, JavaScript, empty result, return view etc. ActionResults is the base class of all result type. ActionResults is abstract class.  As ActionResults is the base class for all type 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

ActionResultHelper MethodDescription
ViewResultViewViewResult Renders a view as a web page.
PartialViewResultPartialViewAs the name describe PartialViewResult renders the partial view.
RedirectResultRedirectWhen you want to redirect to another action method we will use RedirectResult
RedirectToRouteResultRedirectToRouteRedirect to another action method
ContentResultContentReturns a user-defined content type
JsonResultJsonWhen you want to return a serialized JSON object
JavaScriptResultJavaScriptReturns a script that can be executed on the client
FileResultFileReturns 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.
1

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

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

3

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.
4
Learn HTML5 Programming For Beginners
PartialViewResult:
Open Shared folder inside view folder and add new view and rename it to PartialDemo.
5

Open the PartialDemo view and add the below code in it

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

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

Partial

Html.Partial method return the string output, you can put out put 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.

9

Now open ActionDemo view and add below code in it.

Learn the Basics of C Programming Language
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

12

Output
13

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here