Introduction
This article will explain the concept of attributes in ASP.NET MVC. There are many attributes available in ASP.NET MVC that can be applied to ASP.NET MVC model or its properties. We will discuss the following attributes, with examples.
- Display
- DisplayName
- DisplayFormat
- ScaffoldColumn
- DataTypeAttribute,
- DisplayColumnAttribute
- UIHint
- HiddenInput
- ReadOnly
Step 1
Open SQL Server 2014 or a version of your choice and create a table with some data.
Step 2
Choose a "web application" project and give an appropriate name to your project.
Step 3
Select "empty" template, check on the MVC checkbox, and click OK.
Step 4
Right-click 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 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 on 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;
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
-
- namespace MvcAttributes_Demo.Models
- {
- using System;
- using System.Collections.Generic;
-
- [MetadataType(typeof(EmployeeMetaData))]
- public partial class Employee
- {
- [HiddenInput(DisplayValue = false)]
- public int Id { get; set; }
-
- [Required(ErrorMessage = "Please enter name")]
- [DisplayName("Employee Name")]
- public string Name { get; set; }
-
- [DisplayFormat(NullDisplayText = "Not gender specified")]
- public string Gender { get; set; }
-
- [Display(Name = "Phone Number")]
- public string PhoneNumber { get; set; }
-
- [ReadOnly(true)]
- [Display(Name = "Email Address")]
- [DataType(DataType.EmailAddress)]
- public string EmailAddress { get; set; }
-
-
- public string Position { get; set; }
-
- [Display(Name = "Hire Date")]
- [DataType(DataType.Date)]
- public Nullable<System.DateTime> HireDate { get; set; }
-
- [ScaffoldColumn(true)]
- [DataType(DataType.Currency)]
- public Nullable<int> Salary { get; set; }
-
- [Display(Name = "Website")]
- [UIHint("OpenInNewWindow")]
- [DataType(DataType.Url)]
- public string EmployeeWebSite { get; set; }
- }
-
- public class EmployeeMetaData
- {
- [DataType(DataType.Url)]
- public string EmployeeWebSite { get; set; }
- }
- }
If you want the page to open in a new window,
- Right click on Views folder, and add Shared folder if it does not exists.
- Then again, Right click on the Shared folder, and add the DisplayTemplates folder.
- Next, Right-click the DisplayTemplates folder, and add a View. Set OpenInNewWindow as the name.
Write following code in OpenInNewWindow view
- <a href="@ViewData.Model" target="_blank">@ViewData.Model</a>
Step 5
Right-click on Controllers folder and add a new 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 Home Controller
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using MvcAttributes_Demo.Models;
-
- namespace MvcAttributes_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private readonly EmployeeContext _dbContext=new EmployeeContext();
-
- public ActionResult Index()
- {
- var employee = _dbContext.Employees.ToList();
- return View(employee);
- }
-
- [HttpGet]
- public ActionResult Details(int? id)
- {
- if (id==null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
-
- var employee = _dbContext.Employees.Single(e=>e.Id==id);
- if (employee == null)
- {
- return HttpNotFound();
- }
-
- return View(employee);
- }
- }
- }
Step 6
Right-click on Index method in HomeController. The "Add View" window will appear with default index name checked (use a Layout page). Click on "Add".
Code for Index View
- @model IEnumerable<MvcAttributes_Demo.Models.Employee>
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>List of Employee</h2>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Gender)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.PhoneNumber)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.EmailAddress)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Position)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.HireDate)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Salary)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.EmployeeWebSite)
- </th>
- <th>
- Action(s)
- </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.PhoneNumber)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.EmailAddress)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Position)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.HireDate)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Salary)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.EmployeeWebSite)
- </td>
- <td>
- @Html.ActionLink("Details", "Details", new { id=item.Id })
- </td>
- </tr>
- }
-
- </table>
Code for Details View
- @model MvcAttributes_Demo.Models.Employee
- @{
- ViewBag.Title = "Details";
- }
-
- <h2>Employee Details</h2>
- <dl class="dl-horizontal">
- <dt>
- @Html.DisplayNameFor(m => m.Name)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.Name)
- </dd>
- <dt>
- @Html.DisplayNameFor(m => m.Gender)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.Gender)
- </dd>
- <dt>
- @Html.DisplayNameFor(m => m.PhoneNumber)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.PhoneNumber)
- </dd>
- <dt>
- @Html.DisplayNameFor(m => m.EmailAddress)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.EmailAddress)
- </dd>
- <dt>
- @Html.DisplayNameFor(m => m.Position)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.Position)
- </dd>
- <dt>@Html.DisplayNameFor(m => m.HireDate)</dt>
- <dd>
- @Html.DisplayFor(m => m.HireDate)
- </dd>
- <dt>
- @Html.DisplayNameFor(m => m.Salary)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.Salary)
- </dd>
- <dt>
- @Html.DisplayNameFor(m => m.EmployeeWebSite)
- </dt>
- <dd>
- @Html.DisplayFor(m => m.EmployeeWebSite)
- </dd>
- </dl>
Step 7
Build and run the project by pressing Ctrl+F5.
That's it. I hope now you understand how attributes work in MVC.