2014-08-31 38 views
4

我有,其中包括一個虛擬列表,它是另一個類的鏈接一個實體類:MVC DataAnnotation顯示名稱不工作的虛擬物品

 public virtual Employee Employee1 { get; set; } 

所以在編輯頁面我有我的項目有標籤Employee1當我想「我的員工」,所以在我的DataAnnotation類中添加

[DisplayName("My Employee")] 
public virtual Employee Employee1 { get; set; } 

但它還沒有工作。 DataAnnotation類中的其他項目運行良好。

+0

這可能會幫助:https://stackoverflow.com/questions/6405937/using-properties-settings-default-as-an-argument-for-displayname – drneel 2014-08-31 17:55:48

+0

根據一來自Joshua Hayes的回答(http://stackoverflow.com/questions/2555759/wondering-why-displayname-attribute-is-ignored-in-labelfor-on-an-overridden-prop)使用DisplayAttribute而不是DisplayNameAttribute需要小心這個問題。 – Wouter 2014-08-31 18:03:13

回答

4

這是行不通的,因爲Visual Studio時自動創建視圖,外鍵使用Labelfor代替DisplaynameFor象下面這樣:

  @Html.LabelFor(model => model.Employee1.LastName, "Employee1") 

因此,閱讀從第二個參數,而不是DataAnnotation該值,所以我們需要去改變它到:

  @Html.LabelFor(model => model.Employee1.LastName, "My Employee") 
0

您需要在原始類屬性中提供DataAnnotation。如果您將DataAnnotation提供給虛擬財產,它不起作用。我測試過了。但是,如果您將DataAnnotation提供給原始類中的屬性。有用。

如:

public partial class Languages 
{ 
    DataAnnotation here 
    public string Code { get; set; } 
    public string Name { get; set; } 
} 



public partial class Categories 
{ 

    public string Code { get; set; } 
    public string FKLanguageCode { get; set; } 
    public string Name { get; set; } 
    public Nullable<int> Order { get; set; } 

    DataAnnotation Not Here 
    public virtual Languages Languages { get; set; } 
}