ModelMetadata for complex type in editortemplate in asp.net mvc

送分小仙女□ 提交于 2019-12-06 02:57:19

问题


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; }
}

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.

Is there an alternative?


回答1:


You could fetch the metadata for each property like this:

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



回答2:


1) Check this out.

@Html.TextBoxFor
     (m => m.Test1 , 
        new {  
              @placeholder =  
              @ModelMetadata.FromLambdaExpression 
                  (m=>m.Test1 ,ViewData).Watermark.ToString()
            }
     )


来源:https://stackoverflow.com/questions/5336920/modelmetadata-for-complex-type-in-editortemplate-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!