get value from custom attribute in editor template
Asked Answered
I

1

10

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 ?

Indulge answered 30/9, 2010 at 8:31 Comment(1)
Here's a blog post that you might find useful.Grassland
A
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   
    }
}
Amazonite answered 11/9, 2014 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.