Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 41 - Using datatype and displaycolumn attributes in mvc application

Suggested Videos 
Part 38 - CheckBoxList in mvc
Part 39 - ListBox in mvc
Part 40 - Using displayname, displayformat, & scaffoldcolumn attributes

In this video, we will discuss using datatype and displaycolumn attributes. Please watch Part 40, before proceeding.

To understand DataType attribute, we will be using the same example, we used in Part 40. Copy and paste the following code in "Employee" class, and navigate to localhost/MVCDemo/Home/Details/1.
public partial class Employee

public class EmployeeMetaData
    // Display mailto hyperlink
    public string EmailAddress { get; set; }

    // Display currency symbol. For country specific currency, set 
    // culture using globalization element in web.config. 
    // For Great Britain Pound symbol
    // <globalization culture="en-gb"/>
    public int? Salary { get; set; }

    // Generate a hyperlink
    public string PersonalWebSite { get; set; }

    // Display only Time Part
    // [DataType(DataType.Time)]
    // Display only Date Part
    public DateTime? HireDate { get; set; }

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.

Right click on the "Models" folder and add Company.cs class file. Copy and paste the following code. 
public class Company
    public Employee CompanyDirector
            SampleDBContext db = new SampleDBContext();
            return db.Employees.Single(x => x.Id == 1);

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

Decorate "Employee" partial class in "Models" folder, with DisplayColumn attribute.
public partial class Employee

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)

Navigate to localhost/MVCDemo/Home/Details/1 and you should see the FullName of the employee.


  1. Replies
    1. This comment has been removed by the author.

  2. The culture for India is en-IN.
    You can add the following line under system.web tag in web.config file.
    globalization uiCulture="en" culture="en-IN"
    You would see the currency as Rs. 45,000 for the above example.

  3. in DisplayColumn what if I want to display 2 properties from Employee class?

  4. I am using datatype for email address but its not working .Even its not showing any error just getting the previous output only ie; email id without hyperlink.
    Tried number of times..but didnt made it..plz help .Iam not able to move further because of this problem..


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.