如果我有一个包含局部文件的模型
[MetadataType(typeof(ClassAMetadata))]
public partial class ClassA: IStructualObject
{
}
public class ClassAMetadata
{
[DisplayName("Var B:")]
public object VarB { get; set; }
}Var B是一个DateTime!
然后我有一个使用ClassA的控制器的ViewModel (ClassAViewwModel),在这个ViewModel中,我有一个ClassA,它还有一个DateTimeViewModel VarB,原因是在我的应用程序中,DateTime显示为3个字段Date、Hour和Minute。
然后我为我的DateTimeViewModel创建了一个模板。请参阅代码:
@model ViewModels.DateTimeViewModel
<div >
<span class="editor-label">
@Html.LabelFor(m => m)
</span>
<span class="editor-field">
@Html.TextBoxFor(m => m.Date, new { id = "participating_airborne_@ViewData_" + ViewData["cssname"] + "_resource_date", @class="datepicker dato", maxlength="10"})
</span>
<span class="editor-field">
@Html.TextBoxFor(m => m.Hour, new { id = "participating_airborne_" + ViewData["cssname"] + "_resource_hours", @class = "time", maxlength = "2" })
</span>:<span class="editor-field">
@Html.TextBoxFor(m => m.Minute, new { id = "participating_airborne_" + ViewData["cssname"] + "_resource_minutes", @class = "time", maxlength = "2" })
</span>
<span>
@Html.ValidationMessageFor(m => m.Date)
@Html.ValidationMessageFor(m => m.Hour)
@Html.ValidationMessageFor(m => m.Minute)
</span>
</div>然后我想要的是以某种方式从我的VarB中获得DisplayName,当我在我的DateTimeViewModel (或类似的东西)上执行@Html.LabelFor(m => m)时显示出来,所以我需要一种方法来读取部分信息,这可能吗?
发布于 2011-11-10 17:44:31
您应该能够从模型元数据中获取当前模型的显示名称,如下所示:
ViewData.ModelMetadata.GetDisplayName()编辑:
如果您有视图模型:
public class ClassA
{
[DisplayName("Var B:")]
public DateTimeViewModel VarB { get; set; }
}然后在ClassA视图中呈现该属性,如:
@Html.EditorFor(x => x.VarB)然后,它应该使用您的DateTimeViewModel模板,并且能够从模型元数据中提取显示名称。
https://stackoverflow.com/questions/8076167
复制相似问题