Display Attribute Not Working With ASP.NET MVC 2 RTM

Recently I was asked about a strange problem concerning the Display attribute in ASP.NET MVC 2. The problem was the Display atribute had no effect in the view. It was really a strange one. I tried it myself and it was true.

Let’s have a look at that situation. Below is the model for a user.

Using Html.DisplayForModel() in our view results in the following web page:

As you can see the Display attribute has not been taken into consideration at all. The reason for this is ASP.NET MVC 2 RTN do not know about it. Display attribute is new for .NET 4 and ASP.NET MVC 2 RTM is compiled under .NET 3.5. This attribute is supported in Futures release of ASP.NET MVC and will be supported in any newer releases (like MVC 3).

OK, but I didn’t give you a working solution so far. Well, there is – DisplayName attribute. Note that it resides in System.ComponentModel not in System.ComponentModel.DataAnnotations. Our model now looks like this: