Click here to Skip to main content
12,446,110 members (76,560 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15K views
6 bookmarked
Posted

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

, 25 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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.

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

MVC11

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

@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)

Share

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 http://BestTEchnologyBlog.com. 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...

Pro
Pro

Comments and Discussions

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