Different Types Of ActionResult In MVC

Introduction

ASP.NET MVC has different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output.

  • ViewResult (View)
    This return type is used to return a webpage from an action method.

  • PartialViewResult (PartialView)
    This return type is used to send a part of a view which will be rendered in another view.

  • JsonResult (json)
    This return type is used when we want to return a JSON message.

  • JavaScriptResult (JavaScript)
    This return type is used to return JavaScript code that will run in the browser.

  • EmptyResult
    This return type is used to return nothing (void) in the result.

  • ContentResult (Content)
    This return type is used to return HTTP content type like text/plain as the result of the action.

  • RedirectResult (Redirect)
    This return type is used to redirect to any other controller and action method depending on the URL.

  • RedirectToRouteResult (RedirectToAction, RedirectToRoute)
    This return type is used when we want to redirect to any other action method.

  • FileResult (File)
    This return type is used to send binary output in response.

What is action in MVC?

Actions are the methods in a controller class and they are responsible for returning the view or JSON data. Action will mainly have return type   “ActionResult” and it will be invoked from method InvokeAction called by the controller.

What is the importance of NonActionAttribute?

All public methods of a controller class are treated as the action method; if you want to prevent this default method then you have to assign the public method with NonActionAttribute.

Step 1

Open Visual Studio 2015 or your choice and create a new project.

Step 2

Choose web application project and give an appropriate name to your project.
 
Different Types Of ActionResult In MVC 

Step 3

Select empty template, check on MVC checkbox below, and click OK.
 
Different Types Of ActionResult In MVC 

Step 4

Right click on the Models folder and add the database model. Add Entity Framework now. For that, right-click on Models folder, select Add, then select New Item.
 
Different Types Of ActionResult In MVC 

You will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name EmployeeModel (this name is not mandatory, you can give any name) and click "Add".

Different Types Of ActionResult In MVC 

After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".

Different Types Of ActionResult In MVC 

After clicking on "Next", a window will appear. Choose New Connection. Another window will appear. Add your server name - if it is local, then enter dot (.). Choose your database and click "OK".

Different Types Of ActionResult In MVC 

The connection will be added. If you wish, save the connection name as you want. You can change the name of your connection below. It will save the connection in the web config. Now, click "Next"

Different Types Of ActionResult In MVC 

After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".

Different Types Of ActionResult In MVC 

Entity Framework gets added and the respective class gets generated under the Models folder.

Different Types Of ActionResult In MVC 

Step 5

Right click on controllers folder add controller.
 
Different Types Of ActionResult In MVC 

A window will appear. Choose MVC5 Controller-Empty and click "Add".

Different Types Of ActionResult In MVC 

After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home;

Different Types Of ActionResult In MVC 

Complete code for Home Controller

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcActionResults_Demo.Models;  
  7.    
  8. namespace MvcActionResults_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         private readonly EmployeeContext _dbContext=new EmployeeContext();  
  13.    
  14.         public ActionResult Index()  
  15.         {  
  16.             var employee = _dbContext.Employees.ToList();  
  17.             return View(employee);  
  18.         }  
  19.    
  20.         public ViewResult ViewResult()  
  21.         {  
  22.             return View("Index");  
  23.         }  
  24.    
  25.         public PartialViewResult EmployeePartial()  
  26.         {  
  27.             var employees = _dbContext.Employees.ToList();  
  28.             return PartialView("_employee", employees);  
  29.         }  
  30.    
  31.         public JsonResult JsonEmployee()  
  32.         {  
  33.             var empData = _dbContext.Employees.ToList();  
  34.             return Json(new {data = empData}, JsonRequestBehavior.AllowGet);  
  35.         }  
  36.    
  37.         public JavaScriptResult HelloWorld()  
  38.         {  
  39.             var message = "alert('Hollo World, Welcome to programming');";  
  40.             return JavaScript(message);  
  41.         }  
  42.    
  43.         public ContentResult Content()  
  44.         {  
  45.             return Content("<h1>Hello, I am from Content Result</h1>");  
  46.         }  
  47.    
  48.         public EmptyResult Empty()  
  49.         {  
  50.             return new EmptyResult();  
  51.         }  
  52.    
  53.         public FileResult ReadFile()  
  54.         {  
  55.             return File(Url.Content("~/Uploads/MVC topics.pdf"), "MVC topics.pdf");  
  56.         }  
  57.    
  58.         public RedirectResult MyProfile()  
  59.         {  
  60.             return Redirect(url: "https://www.c-sharpcorner.com/members/farhan-ahmed24");  
  61.         }  
  62.    
  63.         public RedirectToRouteResult Department()  
  64.         {  
  65.             return RedirectToRoute(new {controller = "Department", action = "Index"});  
  66.         }  
  67.    
  68.         public ActionResult Action()  
  69.         {  
  70.             return RedirectToAction("MyProfile");  
  71.         }  
  72.     }  
  73. }  

Step 6

Right click on Index method in HomeController The "Add View" window will appear with default index name checked (use a Layout page), and click on "Add.
 
Different Types Of ActionResult In MVC 

Step 7

Right click on the Shared folder under views folder add view. Name it _employee, check on create as a partial view, and click on "Add",

Code for partial view

  1. @model IEnumerable<MvcActionResults_Demo.Models.Employee>  
  2.    
  3. <table class="table">  
  4.     <thead>  
  5.     <tr>  
  6.         <th>@Html.DisplayNameFor(m=>m.Name)</th>  
  7.         <th>@Html.DisplayNameFor(m=>m.Gender)</th>  
  8.         <th>@Html.DisplayNameFor(m=>m.Age)</th>  
  9.         <th>@Html.DisplayNameFor(m=>m.Position)</th>  
  10.         <th>@Html.DisplayNameFor(m=>m.Office)</th>  
  11.         <th>@Html.DisplayNameFor(m=>m.HireDate)</th>  
  12.         <th>@Html.DisplayNameFor(m=>m.Salary)</th>  
  13.     </tr>  
  14.     </thead>  
  15.     <tbody>  
  16.     @foreach(var emp in Model)  
  17.     {  
  18.         <tr>  
  19.             <td>@emp.Name</td>  
  20.             <td>@emp.Gender</td>  
  21.             <td>@emp.Age</td>  
  22.             <td>@emp.Position</td>  
  23.             <td>@emp.Office</td>  
  24.             <td>  
  25.                 @if (emp.HireDate != null)  
  26.                 {  
  27.                     @emp.HireDate  
  28.                 }  
  29.             </td>  
  30.             <td>@emp.Salary</td>  
  31.         </tr>  
  32.     }  
  33.     </tbody>  
  34. </table>  

Step 8

Render partial view in index.
  1. @Html.Partial("_employee")  

Step 9

Call JavaScriptResult in index
  1. <script type="text/javascript" src="@Url.Content("/Home/HelloWorld")"></script>  

Step 10

Create a folder, Uploads, and add some pdf or any file for FileResult action to read file.
  1. public FileResult ReadFile()  
  2. {  
  3.     return File(Url.Content("~/Uploads/MVC topics.pdf"), "MVC topics.pdf");  
  4. }  

Step 11

Create department controller for RedirectToRoute action.
  1. public RedirectToRouteResult Department()  
  2. {  
  3.     return RedirectToRoute(new {controller = "Department", action = "Index"});  
  4. }  


Similar Articles