Action Verb Selector in ASP.NET MVC

Action Verb Selector in ASP.NET MVC Application

In this article, I am going to discuss the Action Verb Selector in ASP.NET MVC Application. Please read our previous article before proceeding to this article as we are going to work with the same example. In our previous article, we discussed the Action Name Selector in ASP.NET MVC Application with some examples. The Action Vern Selector is another selector that we can apply on the Action method of a Controller.

Action Verb Selector in ASP.NET MVC Application:

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.

Database Required:

We are going to use the following Employee table in this demo.

Action Verb Selector in ASP.NET MVC Application

Please use the below SQL Script to create the Employee table with the required test data.

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)
Adding ADO.NET Entity Data Model:

Right-click on the Models folder then select Add => New Item from the context menu. Then select ADO.NET Entity Data Model, provide a meaningful name such as EmployeeDataModel and click on the Add button as shown in the image below.

Adding ADO.NET Entity Data Model in Action Verb Selector

Here, we are going to use Entity Framework Database First Approach to interact with the database. So, from the Choose Entity Model Wizard window, select Generate From Database and click on the Next button as shown below.

Entity Framework Database First Apprach in ASP.NET MVC Application

On the next screen, click on the New Connection tab and provide the necessary details to interact with the database. Then select the database and click on the OK button as shown below.

ASP.NET MVC Action Verb Selector

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

Action Verb Selector in MVC

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

Selecting Database Objects in Entity Framework Database First Approach

Adding Employee Controller:

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 the 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 the GET request. In our example Index is not decorated with any action verb so it by default responds to the 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);
    }
}

In the next article, I am going to discuss Non-Action Selector in ASP.NET MVC Application with examples. Here, in this article, I try to explain the Action Verb Selector in ASP.NET MVC application step by step with a real-time example. I hope this article will help you with your needs. 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 *