2010-09-30 14 views
9

में कस्टम विशेषता से मूल्य प्राप्त मैं इस है:पल में संपादक टेम्पलेट

ViewModel में

:

<%= 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 -मोडेल-विशेषता-amp-editortemplate.aspx) कि आप उपयोगी पा सकते हैं। –

उत्तर

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 
    } 
} 
संबंधित मुद्दे