Using Display/DisplayName attributes in MVC

Introduction

In this article I will post a small example of using Display and DisplayName attribute. Today when I saw a junior developer struggling with DisplayName attribute I thought it to blog it for others also.

What is Display/DisplayName Attribute?

It defines the text we want to show on UI as form fields and Validation Messages.

Why we need Display/DisplayName Attribute?

We will discuss this with an example. Lets assume we have a model with two properties FirstName and ListBoxValue.

public class Employee
{
public string FirstName { get; set; }
public string ListBoxValue { get; set; }
}

This is how our view looks like:

razorview

Now when this model will be rendered on the view as seen below, looks a bit ugly and unformated.

View

For such scenarios only we have two built in attributes: Display and DisplayName. Using these attributes we can control how the text is rendered on the view. As we can see in the below code snippet, I have added Display Attribute on property named “FirstName” and DisplayName attribute on property named ListBoxValue with the text which I want to be rendered on the view in place of property names.

public class Employee
{
[Display(Name = “First Name”)]
public string FirstName { get; set; }
[DisplayName(“List Box Value”)]
public string ListBoxValue { get; set; }
}

After applying the attributes to our property this is how our View is rendered, which looks good.

changedview

Suppose, If two different views are sharing the same model (for instance, maybe one is for mobile output and one is regular), it could be nice to have the string reside in a single place: as metadata on the ViewModel.

Additionally, if you had an inherited version of the model that necessitated a different display, it could be useful. For instance:

public class BaseViewModel
{
[Display(Name = “Basic Name”)]
public virtual string FirstName { get; set; }
}

public class Employee : BaseViewModel
{
[Display(Name = “First Name”)]
public string FirstName;
[DisplayName(“List Box Value”)]
public string ListBoxValue { get; set; }
}

 Difference between Display and DisplayName

Display attribute should be preferred over DisplayName attribute. The former one comes form DataAnnotations namespace and exposes more properties than the later one, ie. ShortName, Prompt, GroupName, Order, Description, which are used by DataAnnotationsMetadataProvider.

Most importantly, DisplayName accepts only literals, while Display accepts resource type and key, which can be used to provide translations using separate resource files (resx).

Point to remember

The correct way of using DisplayName is on properties with getters and setters because by design MVC Binding applies to properties, not fields.

[Display(Name = “First Name”)]
public string FirstName;

Hope this helps.

Advertisements

One thought on “Using Display/DisplayName attributes in MVC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s