从编辑器模板中的自定义属性获取值

时间:2021-09-23 18:57:34

at the moment I have this:

目前我有这个:

in the ViewModel:

在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 ?

如何从我的自定义属性(MyCustom)中获取值(Foo = 23)到编辑器模板中?

1 个解决方案

#1


10  

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

#1


10  

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