Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

ASP.NET MVC – What Are the Uses of DataType and DisplayColumn Attributes?

5.00/5 (3 votes)
25 May 2014CPOL3 min read 27.4K  
What are the uses of DataType and DisplayColumn attributes?

Introduction

In the last blog post, we discussed about Display, DisplayName, DisplayFormat and ScaffoldColumn attributes in ASP.NET MVC. You can read that article here. In this article, we will continue with DataType and DisplayColumn attributes.

Let’s understand these with an example. I am taking the same example which we have used in the previous article. There, we have seen that while running the application, the email address is showing as a plain text.

MVC15

Instead, we want the email address to be rendered with mailto hyperlink. The advantage of this is that when we click on that hyperlink, it is going to open the default email address of the client automatically.

To achieve this, you simply need to decorate the EmailAddress property within the EmployeeMetaData class with DataType attribute.

MVC1

Let’s run the application. We can now see that Email Address is rendered with a mailto hyperlink.

MVC2

Another scenario where you can use DataType attribute is when you want to associate currency symbols. Look at the page, at the moment Salary is displayed without any symbols. We want to associate a currency symbol with the Salary. Let’s use DataType attribute again for this purpose.

MVC3

Let’s build our solution by pressing Ctrl+Shift+B and refresh the View. We can see that now Salary is displayed with a $ symbol by default in my machine.

MVC4

But instead, we want to associate some other currency symbol, say Indian Rupees with this amount. To achieve this, in Web.config file, specify culture using globalization element.

MVC5

Save the changes and refresh the View. We can now see a Rupees symbol instead of a $ symbol.

MVC6

Now look at the Personal Web Site. At the moment, it is displaying as a plain text. Instead, we want that has to be displayed as a hyperlink. We can achieve this very easily. One simple thing you have to do is decorate that property with DataType attribute.

MVC7

Build the solution and refresh the View, we can see that now Personal Web Site is rendered as a hyperlink. While clicking on the hyperlink, you will be navigated to this blog.

MVC8

Now let’s look at the use of DisplayColumn attribute. DisplayColumn attribute is useful, when a class has a property of complex type and you want to pick one property of this complex object for display purpose. Let’s understand this with an example.

First of all, right click on the Models folder and add Company.cs class file.

MVC9

Copy and paste the following code:

C#
public class Company
    public tblEmployee CompanyDirector
    {
        get
        {
            SampleDBContext db = new SampleDBContext();
            return db.tblEmployees.Single(x => x.Id == 1);
        }
    }
}

MVC10

Notice that this class has CompanyDirector property which returns a tblEmployee object. tblEmployee is a complex type. tblEmployee object has got several properties. If you want FullName to be used for display purpose, then make the following changes.

Decorate tblEmployee partial class in Models folder, with DisplayColumn attribute.

DisplayColumn

Change Details action method in Home controller as shown below.

C#
public ActionResult Details(int id)
{
    Company company = new Company();
    return View(company);
}

MVC11

Copy and paste the following code in Details.cshtml view.

C#
@model MVCDemo.Models.Company 
@{
    ViewBag.Title = "Details";
}
@Html.DisplayTextFor(x => x.CompanyDirector)

MVC12

Now run the application and navigate to localhost:57506/MVCDemo/Home/Details/3. You should see the Full Name of the employee.

MVC13

Reference


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)