Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 从编辑器模板中的自定义属性获取值_Asp.net Mvc_Asp.net Mvc 2_Editortemplates - Fatal编程技术网

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

Asp.net mvc 从编辑器模板中的自定义属性获取值,asp.net-mvc,asp.net-mvc-2,editortemplates,Asp.net Mvc,Asp.net Mvc 2,Editortemplates,目前,我有以下几点: 在ViewModel中: [MyCustom(Foo = 23)] public int CountryId { get; set; } 在编辑器模板中: <%= Html.TextBox("", Model) %> 如何将自定义属性(MyCustom)中的值(Foo=23)输入编辑器模板?在编辑器模板中,您可以按如下方式获取自定义属性的值 @model int @{ var CustomAttributes = (ViewDat

目前,我有以下几点:

在ViewModel中:

[MyCustom(Foo = 23)]
public int CountryId { get; set; }
在编辑器模板中:

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


如何将自定义属性(MyCustom)中的值(Foo=23)输入编辑器模板?

在编辑器模板中,您可以按如下方式获取自定义属性的值

@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   
    }
}
这里有一个你可能会觉得有用的例子。