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.
- ActionName
- ActionVerbs
- 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.
Step 3
Select "empty" template, check on MVC checkbox, and click OK.
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".
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".
After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".
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".
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".
After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".
Entity Framework gets added and the respective class gets generated under the Models folder.
Step 5
Right-click on the Controllers folder add a controller.
A window will appear. Choose MVC5 Controller-Empty and click "Add".
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;
The complete code for HomeController
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcActionSelectors_Demo.Models;
-
- namespace MvcActionSelectors_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private readonly EmployeeContext _dbContext=new EmployeeContext();
-
- [ActionName("EmployeeList")]
- public ActionResult Index()
- {
- var employee = _dbContext.Employees.ToList();
- return View(employee);
- }
- }
- }
Now, for some reason, if we want to use Index as the view name, then modify the controller action method as shown below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcActionSelectors_Demo.Models;
-
- namespace MvcActionSelectors_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private readonly EmployeeContext _dbContext=new EmployeeContext();
-
- [ActionName("EmployeeList")]
- public ActionResult Index()
- {
- var employee = _dbContext.Employees.ToList();
- return View("Index",employee);
- }
- }
- }
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
- @model IEnumerable<MvcActionSelectors_Demo.Models.Employee>
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>List of Employee</h2>
- <table class="table table-bordered">
- <thead>
- <tr>
- <th>@Html.DisplayNameFor(m=>m.Name)</th>
- <th>@Html.DisplayNameFor(m=>m.Gender)</th>
- <th>@Html.DisplayNameFor(m=>m.Age)</th>
- <th>@Html.DisplayNameFor(m=>m.Position)</th>
- <th>@Html.DisplayNameFor(m=>m.Office)</th>
- <th>@Html.DisplayNameFor(m=>m.HireDate)</th>
- <th>@Html.DisplayNameFor(m=>m.Salary)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var emp in Model)
- {
- <tr>
- <td>@emp.Name</td>
- <td>@emp.Gender</td>
- <td>@emp.Age</td>
- <td>@emp.Position</td>
- <td>@emp.Office</td>
- <td>
- @if (emp.HireDate != null)
- {
- @emp.HireDate
- }
- </td>
- <td>@emp.Salary</td>
- </tr>
- }
- </tbody>
- </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
- @model IEnumerable<MvcActionSelectors_Demo.Models.Employee>
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>List of Employee</h2>
- <table class="table table-bordered">
- <thead>
- <tr>
- <th>@Html.DisplayNameFor(m=>m.Name)</th>
- <th>@Html.DisplayNameFor(m=>m.Gender)</th>
- <th>@Html.DisplayNameFor(m=>m.Age)</th>
- <th>@Html.DisplayNameFor(m=>m.Position)</th>
- <th>@Html.DisplayNameFor(m=>m.Office)</th>
- <th>@Html.DisplayNameFor(m=>m.HireDate)</th>
- <th>@Html.DisplayNameFor(m=>m.Salary)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var emp in Model)
- {
- <tr>
- <td>@emp.Name</td>
- <td>@emp.Gender</td>
- <td>@emp.Age</td>
- <td>@emp.Position</td>
- <td>@emp.Office</td>
- <td>
- @if (emp.HireDate != null)
- {
- @emp.HireDate
- }
- </td>
- <td>@emp.Salary</td>
- </tr>
- }
- </tbody>
- </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".
Navigate to the following URL.
http://localhost:56100/Home/EmployeeList