2010-09-30 27 views
9

vào lúc này tôi có điều này:nhận được giá trị từ thuộc tính tùy chỉnh trong trình soạn thảo mẫu

trong ViewModel:

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

trong mẫu Editor:

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

làm thế nào tôi có thể nhận được giá trị (Foo = 23) từ thuộc tính tùy chỉnh của tôi (MyCustom) vào mẫu trình soạn thảo?

+0

Đây là [bài đăng trên blog] (http://weblogs.asp.net/seanmcalinden/archive/2010/06/12/asp-net-mvc-2-auto-complete-textbox-custom-view -model-attribute-amp-editortemplate.aspx) mà bạn có thể thấy hữu ích. –

Trả lời

8

Trong mẫu trình chỉnh sửa, bạn có thể nhận được giá trị của thuộc tính tùy chỉnh như bên dưới.

@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 
    } 
} 
Các vấn đề liên quan