Display DataAnnotation attribute in MVC3 Razor

  1. The Display attribute sets the friendly name for a model property.
  2. The name provided using Display attribute is used to display text for Labels.
  3. The Display attribute also allows us to control the order in which properties will appear on UI.
  4. In order to use this attribute, you have to include System.ComponentModel.DataAnnotation reference.

Example 1 :

In the above code, we are setting the Display name. This name is used as Label text on UI.

Example 2 :

In the preceding code, we have changed the order of top two properties. According to the code, control for LastName property will display at the top followed by FirstName property. When no order is defined then the controls are rendered according to the order of properties in model, when controls are rendered using EditorForModel helper.


As we can see the controls are rendered in order as defined in the model class.

Example 3 :

In the above code, we have specified the Name, and ResourceType. The preceding code expects a Resource file with name Multi.resx having a key First_Name holding the text to be shown on UI.
 The preceding code picks the friendly name or text for label from resource file. This helps in achieving Multi-Language support.

UI :

The First Name : text is picked from the resource file.

Watch Video :


Post a Comment