While trying to find how to get this working I came across a blog post that said neither Description nor Watermark are usable with the present incarnation of the DataAnnotations framework.
I came up with a workaround that looks roughly like this:
(Disclaimer: this code is edited from my compiling version to remove it from a metadata provider built through composition so it may not compile directly without some touchups.)
public class CustomDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<System.Attribute> attributes, System.Type containerType, System.Func<object> modelAccessor, System.Type modelType, string propertyName)
{
var baseModelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
var result = new CustomMetadata(modelMetadataProvider, containerType, modelAccessor, modelType, propertyName, attributes.OfType<DisplayColumnAttribute>().FirstOrDefault(), attributes)
{
TemplateHint = !string.IsNullOrEmpty(templateName) ? templateName : baseModelMetaData.TemplateHint,
HideSurroundingHtml = baseModelMetaData.HideSurroundingHtml,
DataTypeName = baseModelMetaData.DataTypeName,
IsReadOnly = baseModelMetaData.IsReadOnly,
NullDisplayText = baseModelMetaData.NullDisplayText,
DisplayFormatString = baseModelMetaData.DisplayFormatString,
ConvertEmptyStringToNull = baseModelMetaData.ConvertEmptyStringToNull,
EditFormatString = baseModelMetaData.EditFormatString,
ShowForDisplay = baseModelMetaData.ShowForDisplay,
ShowForEdit = baseModelMetaData.ShowForEdit,
DisplayName = baseModelMetaData.DisplayName
};
return result;
}
}
public class CustomMetadata : DataAnnotationsModelMetadata
{
private string _description;
public CustomMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable<Attribute> attributes)
: base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
{
var descAttr = attributes.OfType<DescriptionAttribute>().SingleOrDefault();
_description = descAttr != null ? descAttr.Description : "";
}
// here's the really important part
public override string Description
{
get
{
return _description;
}
set
{
_description = value;
}
}
}
Then in your Global.asax in Application_Start or wherever you register your model metadata providers:
ModelMetadataProviders.Current = new CustomMetadataProvider();