Action Verb Selectors in MVC

Action Verb selectors in ASP.NET MVC

In this article, I am going to discuss Action verb selectors in MVC. Please read our last article before proceeding to this article as we are going to use the same example that we started in Action Selectors article.

In our previous article, we discussed the Action Name Action selector

In this article, we are going to discuss the Action Verb Action selector

In the next article, I am going to discuss NonAction action selector.

Another selector filter that we can apply on the Action of a Controller is the ActionVerbs attributes.

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 a 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 create the following Employee table in the database.
CREATE TABLE Employee(
  [EmployeeId] [int] PRIMARY KEY IDENTITY(1,1),
  [FullName] [nvarchar](100),
  [Gender] [nvarchar](10),
  [EmailAddress] [nvarchar](100),
  [Salary] DECIMAL(18, 2)
)

-- Populate the following test data
Insert into Employee values('Pranaya Rout', 'Male', 'pranayakumar777@gmail.com', 10000)
Insert into Employee values('Bikash Rout', 'Male', 'pranayakumar7@gmail.com', 10000)

Select * from Employee

Action Verb Selectors in ASP.NET MVC

Next, Add ADO.NET Entity Data Model for the Employee table.

Right click on Models folder then Add => New Item

Select ADO.NET data Model, Provide a meaningful name “EmployeeDataModel” and click on ADD button as shown below

Action Verb Selectors in ASP.NET MVC

In the next step From Choose Entity data Model Screen choose Generate From Database and Click Next as shown below

Action Verb Selectors in ASP.NET MVC

In the next screen Click on New Connection and provide the necessary details, Select the database and click on OK as shown below

Action Verb Selectors in ASP.NET MVC

In the next step provide a meaningful name “EmployeeDBContext” for the Connection String that is going to create in Web.config file and click on Next as shown below

Action Verb Selectors in ASP.NET MVC

In the next step From Choose your database objects screen, choose the Employee object, provide the namespace name and click on Finish button as shown below.

Action Verb Selectors in ASP.NET MVC

Then it will create the Employee model as shown below

Action Verb Selectors in ASP.NET MVC

Following is the Folder structure for our EDMX file

Action Verb Selectors in ASP.NET MVC

Following is the auto-generated Employee model generated by Entity Framework

namespace ActionSelectorsinMVC.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Employee
    {
        public int EmployeeId { get; set; }
        public string FullName { get; set; }
        public string Gender { get; set; }
        public string EmailAddress { get; set; }
        public Nullable<decimal> Salary { get; set; }
    }
}

Let’s Add Employee Controller to the Controller Folder and copy and paste the following code.

namespace ActionSelectorsinMVC.Controllers
{
    public class EmployeeController : Controller
    {
        EmployeeDBContext dbContext = new EmployeeDBContext();
        public ActionResult Index()
        {
            List<Employee> ListEmployees = dbContext.Employees.ToList();
            return View(ListEmployees);
        } 

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id)
        {
            Employee employee = dbContext.Employees.Where(x => x.EmployeeId == id).FirstOrDefault();
            return View(employee);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(Employee employee)
        {
            if (ModelState.IsValid)
            {
                dbContext.Entry(employee).State = EntityState.Modified;
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employee);
        }
    }
}
Add Index and Edit view.

Right click on Index view and select Add View and provide the following details and click on the Add button as shown below.

Action Verb Selectors in ASP.NET MVC

Right click on Edit action method and click on Add View and provide the following details and click on the Add button as shown below.

Action Verb Selectors in ASP.NET MVC

In the example below, the “Edit” action method that is decorated with the GET accept verb responds to the GET request, whereas the other “Edit” action method which is decorated with POST accept verb responds to POST request. The default is GET. So, if we don’t decorate an action method with any accept verb, then, by default, the method responds to GET request. In our example Index is not decorated with any action verb so it by default responds to GET attribute.

HttpGet and HttpPost attributes can be used as shown below. This is an alternative to using AcceptVerbs attribute.

public class EmployeeController : Controller
{
    EmployeeDBContext dbContext = new EmployeeDBContext();
    public ActionResult Index()
    {
        List<Employee> ListEmployees = dbContext.Employees.ToList();
        return View(ListEmployees);
    }

    [HttpGet]
    public ActionResult Edit(int id)
    {
        Employee employee = dbContext.Employees.Where(x => x.EmployeeId == id).FirstOrDefault();
        return View(employee);
    }

    [HttpPost]
    public ActionResult Save(Employee employee)
    {
        if (ModelState.IsValid)
        {
            dbContext.Entry(employee).State = EntityState.Modified;
            dbContext.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(employee);
    }
}
SUMMARY

In this article, I try to explain Action Verb Selectors in ASP.NET MVC application step by step with a real-time example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *