In this article, I am going to discuss Built-in Attributes in ASP.NET MVC applications. Please read our previous article where we discussed Creating Custom HTML Helpers in ASP.NET MVC applications. The ASP.NET MVC Framework provides many built-in attributes that can be applied to the Model or its properties. I divided this article into two parts. In this article, we are going to discuss the following attributes.
- Display
- DisplayName
- DisplayFormat
- ScaffoldColumn
- DataTypeAttribute
- DisplayColumnAttribute
And in the next Video, I am going to discuss the following attribute.
- UIHint
- HiddenInput
- ReadOnly
Creating the Database Table:
We are going to use the following Employee table.
Please use below SQL Script to create and populate the Employee table that we are going to use in this Video.
Create an empty ASP.NET MVC Project with the name AttributesInMVC. Generate the ADO.NET Entity Data model for table Employee using the database first approach. Save and build the project. It will create the following Employee Model
Creating Employee Controller:
Right-click on the “Controllers†folder and add a controller with the name “EmployeeController†and then copy and paste the following code in it.
Creating Details View:
Add the details view and then copy and paste the below code in it.
Now, run the application and navigate to the URL http://localhost:61449/Employee/Details/1 and It will display the employee information as shown below.
Notice that the output is not that pretty. We can control the display of data in a view using display attributes that are found in the System.ComponentModel.DataAnnotations namespace. It is not a good idea to add display attributes to the properties of the auto-generated “Employee†class as our changes will be lost if the class is auto-generated again.
Creating Employee Partial Class:
We need to create a partial “Employee†class and we need to decorate that class with the display attributes. So, Right-click on the “Models†folder and then add a class file with the name ModifyEmployee.cs. Once you created the ModifyEmployee.cs then copy and paste the following code in it. The code is self-explained. So, please go through the comment lines.
Make sure to include the following using statements:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
Now Run the application and see the output as shown below.
The DisplayColumn attribute is useful when a class has a property of the complex type, and we want to pick only one property of that complex object for display purposes.
Lets understand this with an example.
Right-click on the “Models†folder and add a class file with the name Company.cs and then copy and paste the below code.
Notice that, this class has CompanyDirector property which returns an Employee object. The employee is a complex type so; Employee object has got several properties. If we want FullName to be used for display purposes, then make the following changes.
Modifying the ModifyEmployee.cs
Modify the ModifyEmployee.cs file as shown below where we decorate the “Employee†partial class with the DisplayColumn attribute.
Change the “Details†action method in the “Employee†controller class as shown below.
Copy and paste the following code in Details.cshtml view
Navigate to http://localhost:61449/Employee/Details and you should see the FullName of the employee.