简体   繁体   中英

MVC 2 - Data Annotation for Label?

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)%>

使用[DisplayName("Your Phone")]而不是[LabelName("Your Phone")]

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM