2011-11-07 51 views
8

是否有註釋來設置屬性的標籤名稱?MVC 2 - 標籤的數據註釋?

例:

public class MyModel 
{ 

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

感謝

然後在視圖中,它會顯示名爲「你的電話」

<%=Html.LabelFor(Model.Phone)%> 

回答

18

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

+2

對於我(MVC 5)只有'[Display(Name =「Something nice」)]'如下所示。並且'[DisplayName]不能編譯。我認爲Intellisense會引導大多數讀者朝着正確的方向發展,但仍然認爲接受的答案應該改爲下面的答案。所以-1從我:)。 – Bart

14

如果你指的是標籤的實際內容顯示在頁面上,是的。這是

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

根據我看到的答案,儘管這是一個老帖子,這裏是瓢: (我使用MVC 5)

DisplayName - 定義我們希望在表單域和驗證消息上使用的文本

但是我會說堅持更好更靈活 - >顯示(名稱=

您可以使用這些

[Display(Name = "Add Comments")] 

其中任何一個使用此命名空間:

using System.ComponentModel.DataAnnotations; 

OR

[DisplayName("Add Comments")] 

這是使用該命名空間:

using System.ComponentModel;