I’m trying to emulate the Html Helper for “LabelFor” to work with the [Description] Attribute. I’m having a lot of trouble figuring out how to get the property from the helper though. This is the current signature…
class Something
{
[Description("Simple Description")]
[DisplayName("This is a Display Name, not a Description!")]
public string Name { get; set; }
}
public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
This is not so simple since the
DataAnnotationsModelMetadataProvider(what a name!) does not use the Description attribute.Therefore, to use the Description attribute you will need to do the following:
ModelMetaDataProvider.DescriptionFormethod.So…
Here is the custom
ModelMetaDataProvider:Now, registering it is done in the Global.asax file within the Application_Start method as follows:
And eventually, the implementation of the
DescriptionFormethod:This should work, I checked it on my machine.
Shay.