Click here to Skip to main content
13,000,022 members (47,377 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 25 May 2014

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

Rate this:
Please Sign up or sign in to vote.
What are the uses of DataType and DisplayColumn attributes?


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.


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.


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


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.


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.


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.


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


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.


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.


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.


Copy and paste the following code:

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


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.


Change Details action method in Home controller as shown below.

public ActionResult Details(int id)
    Company company = new Company();
    return View(company);


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

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


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




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


About the Author

Arun Ramachandran India
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 25 May 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid