MVC 2 - Data Annotation for Label?

14,505

Solution 1

Use [DisplayName("Your Phone")] instead of [LabelName("Your Phone")]

Solution 2

If you mean the actual content of the label to be displayed on the page, yes. It is

[Display (Name= "Your Display content")]

Solution 3

Based on the answers I see, while this is an old post, here is the scoop: (I'm using MVC 5)

DisplayName – Defines the text we want used on form fields and validation messages

However I would say to stick with the better and more flexible --> Display(Name =

You CAN use either of these

[Display(Name = "Add Comments")]

which is using this namespace:

using System.ComponentModel.DataAnnotations;

OR

[DisplayName("Add Comments")]

which is using this namespace:

using System.ComponentModel;
Share:
14,505
WoF_Angel
Author by

WoF_Angel

Updated on June 04, 2022

Comments

  • WoF_Angel
    WoF_Angel almost 2 years

    Is there an annotation to set the Label-name of the property?

    Ex:

    public class MyModel
    {
    
    [LabelName("Your Phone")]
    public string Phone{get;set;}
    }
    

    Thanks

    And then in the view, it would show the name "Your Phone"

    <%=Html.LabelFor(Model.Phone)%>
    
  • Bart
    Bart over 9 years
    For me (MVC 5) only [Display(Name="Something nice")] works as given below. And [DisplayName does not compile. I figure Intellisense will guide most readers in the right direction, but still think the accepted answer should be changed to the answer below. So -1 from me :).