2010-09-30 3 views
9

au moment où j'ai ceci:obtenir la valeur d'attribut personnalisé dans le modèle de l'éditeur

dans le ViewModel:

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

dans le modèle Editeur:

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

comment puis-je obtenir le valeur (Foo = 23) de mon attribut personnalisé (MyCustom) dans le modèle de l'éditeur?

+0

[blog] Voici un (http://weblogs.asp.net/seanmcalinden/archive/2010/06/12/asp-net-mvc-2-auto-complete-textbox-custom-view -model-attribute-amp-editortemplate.aspx) que vous pourriez trouver utile. –

Répondre

8

Dans le modèle d'édition, vous pouvez obtenir la valeur du attribut personnalisé ci-dessous.

@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 
    } 
} 
Questions connexes