get value from custom attribute in editor template

≯℡__Kan透↙ 提交于 2019-12-03 12:32:16

问题


at the moment I have this:

in the ViewModel:

[MyCustom(Foo = 23)]
public int CountryId { get; set; }

in the Editor template:

<%= Html.TextBox("", Model) %>

how can I get the value (Foo=23) from my custom attribute (MyCustom) into the editor template ?


回答1:


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


来源:https://stackoverflow.com/questions/3828985/get-value-from-custom-attribute-in-editor-template

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