ModelMetadata在editortemplate在asp.net mvc的复杂类型 [英] ModelMetadata for complex type in editortemplate in asp.net mvc

查看:164
本文介绍了ModelMetadata在editortemplate在asp.net mvc的复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图模型,其中包括被声明为类型TestThing的复杂属性:

I have a viewmodel that includes a complex property of type TestThing which is declared as:

public class TestThing
{
        [Display(Name = "String3", Prompt = "String4")]
        public string Test1 { get; set; }

        [Display(Name = "String5", Prompt = "String6")]
        public string Test2 { get; set; }
}

我有这种类型中,我想是能够访问的元数据的每个子属性的EditorTemplate。如果模板是的字符串,例如,我可以通过使用 @ ViewData.ModelMetadata.Watermark 访问的提示短信,但由于它是一个复杂的类型,我不能用这个方法。

I have an EditorTemplate for this type in which I would like to be able to access the meta data for each of the child properties. If the template was for a string for example, I could access the Prompt text by using @ViewData.ModelMetadata.Watermark, but because it is a complex type, I cannot use this method.

是否有其他选择吗?

推荐答案

您可以为这样每个属性获取元数据:

You could fetch the metadata for each property like this:

@{
    var metadata = ModelMetadata
        .FromLambdaExpression<TestThing, string>(x => x.Test2, ViewData);
    var watermak = metadata.Watermark;
}

这篇关于ModelMetadata在editortemplate在asp.net mvc的复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆