Asp .Net MVC DataAnnotation Display Attribute

Download Source

So far, we have looked at the two DataAnnotation attributes, [Required] and [Compare]. Following are the links to these topics in case you have missed them.

In this short blog post we will look at another important DataAnnotaion attribute, [Display].

So far, when we created our forms using the @Html.LabelFor HtmlHelper our Create Asset screen looked something like this.

Without Display Attribute

See how these labels are generated for our input fields. They have the exact same text as their relevant property names. Following is the screen shot of of the actual model class that generates this form.

Model Class

Let's agree that these are not the best names we can give to our labels, not to mention that our Ux designer will not be happy at all the way end result looks like for this form. Display attribute helps you eliminate this exact same problem. Let's go ahead and modify this class as following.

With Display Attribute

Now, recompile the application and run it again to go to the Create view. You will notice that the text you have applied in the Model class Display attribute are now utilized by the @Html.LabelFor HtmlHelper and generates user friendly Labels for our input form fields.

Display Form

We can utilize this DataAnnotation attribute to give a really nice user friendly look to our old boring Input forms.