2010-09-30 65 views
9

我有這樣的:此刻得到自定義屬性值編輯模板

在視圖模型:

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

我怎麼可以得到:

[MyCustom(Foo = 23)] 
public int CountryId { get; set; } 
在編輯模板

將我的自定義屬性(MyCustom)中的值(Foo = 23)添加到編輯器模板中?

+0

這裏有一個[博客文章(http://weblogs.asp.net/seanmcalinden/archive/2010/06/12/asp-net-mvc-2-auto-complete-textbox-custom-view -model-attribute-amp-editortemplate.aspx),你可能會覺得有用。 – 2010-09-30 08:34:52

回答

8

在編輯器模板中,您可以獲得自定義屬性的值,如下所示。

@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 
    } 
} 
相關問題