10

目前我有这个:

在视图模型中:

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

在编辑器模板中:

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

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

4

1 回答 1

10

在编辑器模板中,您可以获得自定义属性的值,如下所示。

@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   
    }
}
于 2014-09-11T06:42:06.250 回答