Extract Display Name and Description Attribute from HTML Helper

I am creating a special HTML.LabelFor helper that looks like this:

 public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, Boolean showToolTip) { var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData); ... } 

To get a custom name for the property, I use the following code:

 metadata.DisplayName 

And by the property of the ModelView class, I got:

 [DisplayName("Titel")] 

The problem is that I also need a description. There is an attribute called Display, which has a name and description, but I donโ€™t see how to extract it using the metadata variable in the above code?

+7
reflection c # attributes html-helper asp.net-mvc-2
Feb 11 2018-11-11T00:
source share
1 answer

Disclaimer: the following only works with ASP.NET MVC 3 (see update below if you are using previous versions)

Assuming the following model:

 public class MyViewModel { [Display(Description = "some description", Name = "some name")] public string SomeProperty { get; set; } } 

And the following view:

 <%= Html.LabelFor(x => x.SomeProperty, true) %> 

Inside your user helper, you can get this information from metadata:

 public static MvcHtmlString LabelFor<TModel, TValue>( this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, bool showToolTip ) { var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData); var description = metadata.Description; // will equal "some description" var name = metadata.DisplayName; // will equal "some name" // TODO: do something with the name and the description ... } 

Note. Having [DisplayName("foo")] and [Display(Name = "bar")] in the same model property is redundant, and the name used in the [Display] attribute takes precedence in metadata.DisplayName .




UPDATE:

My previous answer will not work with ASP.NET MVC 2.0. There are pairs of properties that cannot be populated by default with DataAnnotations in .NET 3.5, and Description is one of them. To achieve this in ASP.NET MVC 2.0, you can use the modelโ€™s own metadata provider:

 public class DisplayMetaDataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata( IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName ) { var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault(); if (displayAttribute != null) { metadata.Description = displayAttribute.Description; metadata.DisplayName = displayAttribute.Name; } return metadata; } } 

which you would register in Application_Start :

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); ModelMetadataProviders.Current = new DisplayMetaDataProvider(); } 

and then the assistant should work as expected:

 public static MvcHtmlString LabelFor<TModel, TValue>( this HtmlHelper<TModel> self, Expression<Func<TModel, TValue>> expression, bool showToolTip ) { var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData); var description = metadata.Description; // will equal "some description" var name = metadata.DisplayName; // will equal "some name" // TODO: do something with the name and the description ... } 
+19
Feb 11 '11 at 20:51
source share



All Articles