2012-02-07 28 views
0

我有一個被定義爲一個視圖模型如下:獲取父的DisplayAttribute物業

public class VariableViewModel 
{ 
    public string TemplateName { get; set; } 
    public bool Enabled { get; set; } 
    public string Value { get; set; } 
} 

我使用這種模式在其他視圖模式:

public class CreateViewModel 
{ 
    [Display(Name="First Name")] 
    public VariableViewModel FirstName { get; set; } 

    [Display(Name="Last Name")] 
    public VariableViewModel LastName { get; set; } 
} 

我有一個編輯模板定義VariableViewModel:

@model VariableViewModel 

@Html.HiddenFor(model => model.TemplateName) 
@Html.HiddenFor(model => model.Enabled) 
@Html.LabelFor(model => model.Value) 
@Html.TextBoxFor(model => model.Value) 

我的CreateViewModel的編輯器模板:

@model CreateViewModel 

@Html.EditorFor(model => model.FirstName) 
@Html.EditorFor(model => model.LastName) 

現在我的編輯模板創建標籤如下:

<label for="FirstName">Value</label> 

是否有可能修改LabelFor的方式,它看起來在父屬性DisplayAttribute以確定它應該使用什麼而不是價值?我希望我的標籤,看起來像:

<label for="FirstName">First Name</label> 
<label for="LastName">Last Name</label> 

的問題是,在顯示屬性是不是在我創建該標籤的屬性,但包含的對象上。有沒有辦法實現我想要的?

回答

5

在你的編輯器模板只需使用該標籤的下面:

@Html.LabelFor(model => model.Value, ViewData.ModelMetadata.DisplayName) 

我們傳遞父顯示名稱作爲值的標籤。