ActionName Selectors In ASP.NET MVC

Introduction

Actions are public methods in an MVC controller, that respond to a URL request. Action Selectors are attributes that can be applied to action methods and are used to influence or control which action method gets invoked in response to a request. It helps the routing engine to select the correct action method to handle a particular request.

ASP.NET MVC 5 includes the following action selector attributes.

  1. ActionName
  2. ActionVerbs
  3. NonAction
Let's see them via a demo application.

Step 1

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

Step 2

Choose "web application" project and give an appropriate name to your project.
 
ActionName Selectors In ASP.NET MVC

Step 3

Select "empty" template, check on MVC checkbox, and click OK.
 
ActionName Selectors In ASP.NET MVC

Step 4

Right-click the Models folder and add a database model. Add Entity Framework now. For that, right-click on Models folder, select Add, then select "New Item".
 
ActionName Selectors In ASP.NET 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".

ActionName Selectors In ASP.NET MVC

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

ActionName Selectors In ASP.NET 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 a dot (.). Choose your database and click "OK".

ActionName Selectors In ASP.NET 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".

ActionName Selectors In ASP.NET MVC

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

ActionName Selectors In ASP.NET MVC

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

ActionName Selectors In ASP.NET MVC

Step 5

Right-click on the Controllers folder add a controller.
 
ActionName Selectors In ASP.NET MVC

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

ActionName Selectors In ASP.NET 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;

ActionName Selectors In ASP.NET MVC

The complete code for HomeController

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcActionSelectors_Demo.Models;  
  7.    
  8. namespace MvcActionSelectors_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         private readonly EmployeeContext _dbContext=new EmployeeContext();  
  13.    
  14.         [ActionName("EmployeeList")]  
  15.         public ActionResult Index()  
  16.         {  
  17.             var employee = _dbContext.Employees.ToList();  
  18.             return View(employee);  
  19.         }  
  20.     }  
  21. }  

Now, for some reason, if we want to use Index as the view name, then modify the controller action method as shown below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcActionSelectors_Demo.Models;  
  7.    
  8. namespace MvcActionSelectors_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         private readonly EmployeeContext _dbContext=new EmployeeContext();  
  13.    
  14.         [ActionName("EmployeeList")]  
  15.         public ActionResult Index()  
  16.         {  
  17.             var employee = _dbContext.Employees.ToList();  
  18.             return View("Index",employee);  
  19.         }  
  20.     }  
  21. }  

Step 6

Let’s add two views with the name EmployeeList.cshtml and Index.cshtml within the Home folder, write the following code in both the views. Right-click on Index method in HomeController the "Add View" window will appear with default index name checked (use a Layout page). Just click on "Add.
 

Code for Index View

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

Step 7

Right-click the Home folder under Views folder and add EmployeeList view. The "Add View" window will appear with default index name. Change the name to EmployeeList checked (use a Layout page), and click on "Add.

Code for EmployeeList view

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

Step 7

Build and run the project by pressing Ctrl+F5.

Now, if you navigate to /Home/Index, you will get an error - "The resource cannot be found".

ActionName Selectors In ASP.NET MVC
Navigate to the following URL.

http://localhost:56100/Home/EmployeeList

ActionName Selectors In ASP.NET MVC


Similar Articles