Action Verb Selector in ASP.NET MVC

Action Verb Action Selector in ASP.NET MVC Application

In this article, I am going to discuss the Action Verb Action 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 Action Selector in the ASP.NET MVC Application. The Action Verb Selector is another selector that we can apply to the Action methods of a Controller.

Action Verb Action Selector in ASP.NET MVC Application:

We need to use the Action Verb Selector when we want to control the invocation of an action method based on the request type in the ASP.NET MVC Application. We can define two different action methods with the same name but one action method responds to an HTTP Get request while the other 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 the 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 the 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. In the below code, the “Edit” action method that is decorated with the [AcceptVerbs(HttpVerbs.Get)] accept verb responds to the GET request, whereas the “Edit” action method which is decorated with [AcceptVerbs(HttpVerbs.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.

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 Edit(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

HttpGet and HttpPost Attribute in ASP.NET MVC:

Instead of using [AcceptVerbs(HttpVerbs.Get)] and [AcceptVerbs(HttpVerbs.Post)] attribute, we can also use HttpGet and HttpPost attributes as shown in the below code. This is an alternative approach to using the AcceptVerbs attribute and the behavior is going to be the same i.e. the action method which decorates with the HttpGet attribute will only respond to GET Request whereas the action method which decorates with HttpPost attribute will only respond to the Post Request.

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 Edit(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 Action Verb Action Selector in the ASP.NET MVC 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 *