Action Verb Selectors In ASP.NET MVC

In this article, I am going to discuss the basics of Action Verb selectors in ASP.NET MVC.

Introduction

We need to use this selector when we want to control the invocation of an action method based on the request type. So, this restricts the indication of specific action to specific HttpVerbs. We can define two different action methods with the same name but one action method responds to an HTTP Get request and another action method responds to an HTTP Post request.

Let's see it in action.

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.
 
Action Verb Selectors In ASP.NET MVC

Step 3

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

Step 4

Right-click on 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.
 
Action Verb 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".

Action Verb 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".

Action Verb 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".

Action Verb Selectors In ASP.NET MVC

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

Action Verb Selectors In ASP.NET MVC

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

Action Verb Selectors In ASP.NET MVC

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

Action Verb Selectors In ASP.NET MVC

Employee class

  1. using System.ComponentModel.DataAnnotations;  
  2.    
  3. namespace MvcActionSelectors_Demo.Models  
  4. {  
  5.     using System;  
  6.     using System.Collections.Generic;  
  7.       
  8.     public partial class Employee  
  9.     {  
  10.         public int EmployeeId { getset; }  
  11.    
  12.         [Required(ErrorMessage = "Please enter name")]  
  13.         public string Name { getset; }  
  14.    
  15.         [Required(ErrorMessage = "Please choose gender")]  
  16.         public string Gender { getset; }  
  17.    
  18.         [Required(ErrorMessage = "Please enter age")]  
  19.         public Nullable<int> Age { getset; }  
  20.    
  21.         [Required(ErrorMessage = "Please enter position")]  
  22.         public string Position { getset; }  
  23.    
  24.         [Required(ErrorMessage = "Please enter office")]  
  25.         public string Office { getset; }  
  26.    
  27.         [Required(ErrorMessage = "Please enter hire date")]  
  28.         [Display(Name = "Hire Date")]  
  29.         public Nullable<System.DateTime> HireDate { getset; }  
  30.    
  31.         [Required(ErrorMessage = "Please enter salary")]  
  32.         public Nullable<int> Salary { getset; }  
  33.    
  34.         [Required(ErrorMessage = "Please choose gender")]  
  35.         [Display(Name = "Department Name")]  
  36.         public Nullable<int> DepartmentId { getset; }  
  37.     }  
  38. }  

Step 5

Right-click on Controllers folder and add a controller.
 
Action Verb Selectors In ASP.NET MVC 

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

Action Verb 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;

Action Verb 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.Net;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. using MvcActionSelectors_Demo.Models;  
  8. using System.Data.Entity;  
  9.    
  10.    
  11. namespace MvcActionSelectors_Demo.Controllers  
  12. {  
  13.     public class EmployeeController : Controller  
  14.     {  
  15.         private readonly EmployeeContext _dbContext=new EmployeeContext();  
  16.    
  17.         [AcceptVerbs(HttpVerbs.Get)]  
  18.         public ActionResult Index()  
  19.         {  
  20.             var employee = _dbContext.Employees.Include(e => e.Department).ToList();  
  21.             return View(employee);  
  22.         }  
  23.    
  24.         [AcceptVerbs(HttpVerbs.Get)]  
  25.         public ActionResult Details(int? id)  
  26.         {  
  27.             if (id == null)  
  28.             {  
  29.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  30.             }  
  31.             Employee employee = _dbContext.Employees.Find(id);  
  32.             if (employee == null)  
  33.             {  
  34.                 return HttpNotFound();  
  35.             }  
  36.             return View(employee);  
  37.         }  
  38.    
  39.         public ActionResult Create()  
  40.         {  
  41.             PopulateDepartmentsDropDownList();  
  42.             return View();  
  43.         }  
  44.    
  45.         [AcceptVerbs(HttpVerbs.Post)]  
  46.         [ValidateAntiForgeryToken]  
  47.         public ActionResult Create(Employee employee)  
  48.         {  
  49.             if (ModelState.IsValid)  
  50.             {  
  51.                 _dbContext.Employees.Add(employee);  
  52.                 _dbContext.SaveChanges();  
  53.                 return RedirectToAction("Index");  
  54.             }  
  55.             PopulateDepartmentsDropDownList(employee.DepartmentId);  
  56.             return View();  
  57.         }  
  58.         private void PopulateDepartmentsDropDownList(object selectedDepartment = null)  
  59.         {  
  60.             var departmentsQuery = from d in _dbContext.Departments  
  61.                 orderby d.DepartmentName  
  62.                 select d;  
  63.             ViewBag.DepartmentID = new SelectList(departmentsQuery, "ID""DepartmentName", selectedDepartment);  
  64.         }  
  65.    
  66.     }  
  67. }  

Step 6

Right-click on the Index method in HomeController; the "Add View" window will appear with default index name checked (use a Layout page). Click on "Add. Similarly, add respective views for Details and Create.
 
Action Verb Selectors In ASP.NET MVC

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. <p>  
  9.     @Html.ActionLink("Create New""Create")  
  10. </p>  
  11. <table class="table">  
  12.     <tr>  
  13.         <th>  
  14.             @Html.DisplayNameFor(model => model.Name)  
  15.         </th>  
  16.         <th>  
  17.             @Html.DisplayNameFor(model => model.Gender)  
  18.         </th>  
  19.         <th>  
  20.             @Html.DisplayNameFor(model => model.Age)  
  21.         </th>  
  22.         <th>  
  23.             @Html.DisplayNameFor(model => model.Position)  
  24.         </th>  
  25.         <th>  
  26.             @Html.DisplayNameFor(model => model.Office)  
  27.         </th>  
  28.         <th>  
  29.             @Html.DisplayNameFor(model => model.HireDate)  
  30.         </th>  
  31.         <th>  
  32.             @Html.DisplayNameFor(model => model.Salary)  
  33.         </th>  
  34.         <th>  
  35.             @Html.DisplayNameFor(model => model.Department.DepartmentName)  
  36.         </th>  
  37.         <th></th>  
  38.     </tr>  
  39.    
  40.     @foreach (var item in Model) {  
  41.         <tr>  
  42.             <td>  
  43.                 @Html.DisplayFor(modelItem => item.Name)  
  44.             </td>  
  45.             <td>  
  46.                 @Html.DisplayFor(modelItem => item.Gender)  
  47.             </td>  
  48.             <td>  
  49.                 @Html.DisplayFor(modelItem => item.Age)  
  50.             </td>  
  51.             <td>  
  52.                 @Html.DisplayFor(modelItem => item.Position)  
  53.             </td>  
  54.             <td>  
  55.                 @Html.DisplayFor(modelItem => item.Office)  
  56.             </td>  
  57.             <td>  
  58.                 @Html.DisplayFor(modelItem => item.HireDate)  
  59.             </td>  
  60.             <td>  
  61.                 @Html.DisplayFor(modelItem => item.Salary)  
  62.             </td>  
  63.             <td>  
  64.                 @Html.DisplayFor(modelItem => item.Department.DepartmentName)  
  65.             </td>  
  66.             <td>  
  67.                 @Html.ActionLink("Details""Details"new { id=item.EmployeeId })   
  68.             </td>  
  69.         </tr>  
  70.     }  
  71.    
  72. </table>  

Code for Details View

  1. @model MvcActionSelectors_Demo.Models.Employee  
  2.    
  3. @{  
  4.     ViewBag.Title = "Details";  
  5. }  
  6.    
  7. <h2>Details</h2>  
  8.    
  9. <div>  
  10.     <h4>Employee</h4>  
  11.     <hr />  
  12.     <dl class="dl-horizontal">  
  13.         <dt>  
  14.             @Html.DisplayNameFor(model => model.Name)  
  15.         </dt>  
  16.    
  17.         <dd>  
  18.             @Html.DisplayFor(model => model.Name)  
  19.         </dd>  
  20.    
  21.         <dt>  
  22.             @Html.DisplayNameFor(model => model.Gender)  
  23.         </dt>  
  24.    
  25.         <dd>  
  26.             @Html.DisplayFor(model => model.Gender)  
  27.         </dd>  
  28.    
  29.         <dt>  
  30.             @Html.DisplayNameFor(model => model.Age)  
  31.         </dt>  
  32.    
  33.         <dd>  
  34.             @Html.DisplayFor(model => model.Age)  
  35.         </dd>  
  36.    
  37.         <dt>  
  38.             @Html.DisplayNameFor(model => model.Position)  
  39.         </dt>  
  40.    
  41.         <dd>  
  42.             @Html.DisplayFor(model => model.Position)  
  43.         </dd>  
  44.    
  45.         <dt>  
  46.             @Html.DisplayNameFor(model => model.Office)  
  47.         </dt>  
  48.    
  49.         <dd>  
  50.             @Html.DisplayFor(model => model.Office)  
  51.         </dd>  
  52.    
  53.         <dt>  
  54.             @Html.DisplayNameFor(model => model.HireDate)  
  55.         </dt>  
  56.    
  57.         <dd>  
  58.             @Html.DisplayFor(model => model.HireDate)  
  59.         </dd>  
  60.    
  61.         <dt>  
  62.             @Html.DisplayNameFor(model => model.Salary)  
  63.         </dt>  
  64.    
  65.         <dd>  
  66.             @Html.DisplayFor(model => model.Salary)  
  67.         </dd>  
  68.    
  69.         <dt>  
  70.             @Html.DisplayNameFor(model => model.Department.DepartmentName)  
  71.         </dt>  
  72.    
  73.         <dd>  
  74.             @Html.DisplayFor(model => model.Department.DepartmentName)  
  75.         </dd>  
  76.    
  77.     </dl>  
  78. </div>  
  79. <p>  
  80.     @Html.ActionLink("Edit""Edit"new { id = Model.EmployeeId }) |  
  81.     @Html.ActionLink("Back to List""Index")  
  82. </p>  

Code for Create View

  1. @model MvcActionSelectors_Demo.Models.Employee  
  2.    
  3. @{  
  4.     ViewBag.Title = "Create";  
  5. }  
  6.    
  7. <h2>Create Employee</h2>  
  8.    
  9. @using (Html.BeginForm())   
  10. {  
  11.     @Html.AntiForgeryToken()  
  12.       
  13.     <div class="form-horizontal">  
  14.         <hr />  
  15.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  16.         <div class="form-group">  
  17.             @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })  
  18.             <div class="col-md-10">  
  19.                 @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })  
  20.                 @Html.ValidationMessageFor(model => model.Name, ""new { @class = "text-danger" })  
  21.             </div>  
  22.         </div>  
  23.    
  24.         <div class="form-group">  
  25.             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  26.             <div class="col-md-10">                 
  27.                 @Html.DropDownList("Gender"new List<SelectListItem>()  
  28.                 {  
  29.                     new SelectListItem {Text = "Male", Value = "Male"},  
  30.                     new SelectListItem {Text = "Female", Value = "Female"}  
  31.                 }, "Choose Gender"new { @class = "form-control" })  
  32.    
  33.                 @Html.ValidationMessageFor(model => model.Gender, ""new { @class = "text-danger" })  
  34.             </div>  
  35.         </div>  
  36.    
  37.         <div class="form-group">  
  38.             @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })  
  39.             <div class="col-md-10">  
  40.                 @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })  
  41.                 @Html.ValidationMessageFor(model => model.Age, ""new { @class = "text-danger" })  
  42.             </div>  
  43.         </div>  
  44.    
  45.         <div class="form-group">  
  46.             @Html.LabelFor(model => model.Position, htmlAttributes: new { @class = "control-label col-md-2" })  
  47.             <div class="col-md-10">  
  48.                 @Html.EditorFor(model => model.Position, new { htmlAttributes = new { @class = "form-control" } })  
  49.                 @Html.ValidationMessageFor(model => model.Position, ""new { @class = "text-danger" })  
  50.             </div>  
  51.         </div>  
  52.    
  53.         <div class="form-group">  
  54.             @Html.LabelFor(model => model.Office, htmlAttributes: new { @class = "control-label col-md-2" })  
  55.             <div class="col-md-10">  
  56.                 @Html.EditorFor(model => model.Office, new { htmlAttributes = new { @class = "form-control" } })  
  57.                 @Html.ValidationMessageFor(model => model.Office, ""new { @class = "text-danger" })  
  58.             </div>  
  59.         </div>  
  60.    
  61.         <div class="form-group">  
  62.             @Html.LabelFor(model => model.HireDate, htmlAttributes: new { @class = "control-label col-md-2" })  
  63.             <div class="col-md-10">  
  64.                 @Html.EditorFor(model => model.HireDate, new { htmlAttributes = new { @class = "form-control" } })  
  65.                 @Html.ValidationMessageFor(model => model.HireDate, ""new { @class = "text-danger" })  
  66.             </div>  
  67.         </div>  
  68.    
  69.         <div class="form-group">  
  70.             @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })  
  71.             <div class="col-md-10">  
  72.                 @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })  
  73.                 @Html.ValidationMessageFor(model => model.Salary, ""new { @class = "text-danger" })  
  74.             </div>  
  75.         </div>  
  76.    
  77.         <div class="form-group">  
  78.             @Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })  
  79.             <div class="col-md-10">  
  80.                 @Html.DropDownList("DepartmentID", (IEnumerable<SelectListItem>)ViewBag.DepartmentID, "Choose Department"new { @class = "form-control" })  
  81.                 @Html.ValidationMessageFor(model => model.DepartmentId, ""new { @class = "text-danger" })  
  82.             </div>  
  83.         </div>  
  84.    
  85.         <div class="form-group">  
  86.             <div class="col-md-offset-2 col-md-10">  
  87.                 <input type="submit" value="Create" class="btn btn-default" />  
  88.             </div>  
  89.         </div>  
  90.     </div>  
  91. }  
  92.    
  93. <div>  
  94.     @Html.ActionLink("Back to List""Index")  
  95. </div>  

Step 7

Build and run the project by pressing Ctrl+F5.
 
Action Verb Selectors In ASP.NET MVC
 
Action Verb Selectors In ASP.NET MVC