获得编辑模板从定制属性值 [英] get value from custom attribute in editor template
本文介绍了获得编辑模板从定制属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在现阶段,我有这样的:
at the moment I have this:
在视图模型:
[MyCustom(Foo = 23)]
public int CountryId { get; set; }
在编辑器中的模板:
in the Editor template:
<%= Html.TextBox("", Model) %>
我怎样才能从我的自定义属性(MyCustom)值(美孚= 23)到编辑模板?
how can I get the value (Foo=23) from my custom attribute (MyCustom) into the editor template ?
推荐答案
在编辑器中的模板,你可以得到自定义的值,如下属性。
In Editor Template you can get the value of custom attribute as below.
@model int
@{
var CustomAttributes = (ViewData.ModelMetadata).ContainerType.GetProperty(ViewData.ModelMetadata.PropertyName).GetCustomAttributes(typeof(MvcApplication7.Models.MyCustomAttribute), false);
if (CustomAttributes.Length > 0)
{
MvcApplication7.Models.MyCustomAttribute CustomAttribute = CustomAttributes[0] as MvcApplication7.Models.MyCustomAttribute;
//That is how you get the value of foo. You can use it as per need of the editor template.
@CustomAttribute.Foo
}
}
这篇关于获得编辑模板从定制属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文