Asp.net mvc 获取父属性的DisplayAttribute

Asp.net mvc 获取父属性的DisplayAttribute,asp.net-mvc,Asp.net Mvc,我有一个视图模型,定义如下: public class VariableViewModel { public string TemplateName { get; set; } public bool Enabled { get; set; } public string Value { get; set; } } <label for="FirstName">Value</label> 我在其他视图模型中使用此模型: public class

我有一个视图模型,定义如下:

public class VariableViewModel
{
    public string TemplateName { get; set; }
    public bool Enabled { get; set; }
    public string Value { get; set; }
}
<label for="FirstName">Value</label>
我在其他视图模型中使用此模型:

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)
@model CreateViewModel

@Html.EditorFor(model => model.FirstName)
@Html.EditorFor(model => model.LastName)
我的CreateViewModel的编辑器模板:

@model VariableViewModel

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

@Html.EditorFor(model => model.FirstName)
@Html.EditorFor(model => model.LastName)
现在,我的编辑器模板正在创建标签,如下所示:

public class VariableViewModel
{
    public string TemplateName { get; set; }
    public bool Enabled { get; set; }
    public string Value { get; set; }
}
<label for="FirstName">Value</label>

问题在于,“显示”属性不在为其创建标签的属性上,而是在包含该属性的对象上。有什么方法可以实现我想要的吗?

在编辑器模板中,只需使用以下标签:

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

我们正在将父显示名称作为标签的值传递。

在编辑器模板中,只需将以下内容用于标签:

@Html.LabelFor(model => model.Value, ViewData.ModelMetadata.DisplayName)
我们正在传递父显示名称作为标签的值