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.
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 below, and click OK.
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.
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 whatever 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 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.
Employee class
- using System.ComponentModel.DataAnnotations;
-
- namespace MvcActionSelectors_Demo.Models
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Employee
- {
- public int EmployeeId { get; set; }
-
- [Required(ErrorMessage = "Please enter name")]
- public string Name { get; set; }
-
- [Required(ErrorMessage = "Please choose gender")]
- public string Gender { get; set; }
-
- [Required(ErrorMessage = "Please enter age")]
- public Nullable<int> Age { get; set; }
-
- [Required(ErrorMessage = "Please enter position")]
- public string Position { get; set; }
-
- [Required(ErrorMessage = "Please enter office")]
- public string Office { get; set; }
-
- [Required(ErrorMessage = "Please enter hire date")]
- [Display(Name = "Hire Date")]
- public Nullable<System.DateTime> HireDate { get; set; }
-
- [Required(ErrorMessage = "Please enter salary")]
- public Nullable<int> Salary { get; set; }
-
- [Required(ErrorMessage = "Please choose gender")]
- [Display(Name = "Department Name")]
- public Nullable<int> DepartmentId { get; set; }
- }
- }
Step 5
Right-click on Controllers folder and 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.Net;
- using System.Web;
- using System.Web.Mvc;
- using MvcActionSelectors_Demo.Models;
- using System.Data.Entity;
-
-
- namespace MvcActionSelectors_Demo.Controllers
- {
- public class EmployeeController : Controller
- {
- private readonly EmployeeContext _dbContext=new EmployeeContext();
-
- [AcceptVerbs(HttpVerbs.Get)]
- public ActionResult Index()
- {
- var employee = _dbContext.Employees.Include(e => e.Department).ToList();
- return View(employee);
- }
-
- [AcceptVerbs(HttpVerbs.Get)]
- public ActionResult Details(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Employee employee = _dbContext.Employees.Find(id);
- if (employee == null)
- {
- return HttpNotFound();
- }
- return View(employee);
- }
-
- public ActionResult Create()
- {
- PopulateDepartmentsDropDownList();
- return View();
- }
-
- [AcceptVerbs(HttpVerbs.Post)]
- [ValidateAntiForgeryToken]
- public ActionResult Create(Employee employee)
- {
- if (ModelState.IsValid)
- {
- _dbContext.Employees.Add(employee);
- _dbContext.SaveChanges();
- return RedirectToAction("Index");
- }
- PopulateDepartmentsDropDownList(employee.DepartmentId);
- return View();
- }
- private void PopulateDepartmentsDropDownList(object selectedDepartment = null)
- {
- var departmentsQuery = from d in _dbContext.Departments
- orderby d.DepartmentName
- select d;
- ViewBag.DepartmentID = new SelectList(departmentsQuery, "ID", "DepartmentName", selectedDepartment);
- }
-
- }
- }
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.
Code for Index View
- @model IEnumerable<MvcActionSelectors_Demo.Models.Employee>
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>List of Employee</h2>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Gender)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Age)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Position)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Office)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.HireDate)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Salary)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Department.DepartmentName)
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Gender)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Age)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Position)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Office)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.HireDate)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Salary)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Department.DepartmentName)
- </td>
- <td>
- @Html.ActionLink("Details", "Details", new { id=item.EmployeeId })
- </td>
- </tr>
- }
-
- </table>
Code for Details View
- @model MvcActionSelectors_Demo.Models.Employee
-
- @{
- ViewBag.Title = "Details";
- }
-
- <h2>Details</h2>
-
- <div>
- <h4>Employee</h4>
- <hr />
- <dl class="dl-horizontal">
- <dt>
- @Html.DisplayNameFor(model => model.Name)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Name)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Gender)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Gender)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Age)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Age)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Position)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Position)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Office)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Office)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.HireDate)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.HireDate)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Salary)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Salary)
- </dd>
-
- <dt>
- @Html.DisplayNameFor(model => model.Department.DepartmentName)
- </dt>
-
- <dd>
- @Html.DisplayFor(model => model.Department.DepartmentName)
- </dd>
-
- </dl>
- </div>
- <p>
- @Html.ActionLink("Edit", "Edit", new { id = Model.EmployeeId }) |
- @Html.ActionLink("Back to List", "Index")
- </p>
Code for Create View
- @model MvcActionSelectors_Demo.Models.Employee
-
- @{
- ViewBag.Title = "Create";
- }
-
- <h2>Create Employee</h2>
-
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
-
- <div class="form-horizontal">
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.DropDownList("Gender", new List<SelectListItem>()
- {
- new SelectListItem {Text = "Male", Value = "Male"},
- new SelectListItem {Text = "Female", Value = "Female"}
- }, "Choose Gender", new { @class = "form-control" })
-
- @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Position, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Position, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Position, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Office, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Office, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Office, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.HireDate, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.HireDate, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.HireDate, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- @Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.DropDownList("DepartmentID", (IEnumerable<SelectListItem>)ViewBag.DepartmentID, "Choose Department", new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
- </div>
- </div>
-
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
-
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
Step 7
Build and run the project by pressing Ctrl+F5.