Asp.net mvc 从编辑器模板中的自定义属性获取值
目前,我有以下几点: 在ViewModel中: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
[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
}
}
这里有一个你可能会觉得有用的例子。