Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>I came up with a workaround that looks roughly like this:</p> <p>(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.)</p> <pre><code>public class CustomDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata(IEnumerable&lt;System.Attribute&gt; attributes, System.Type containerType, System.Func&lt;object&gt; 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&lt;DisplayColumnAttribute&gt;().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&lt;object&gt; modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable&lt;Attribute&gt; attributes) : base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute) { var descAttr = attributes.OfType&lt;DescriptionAttribute&gt;().SingleOrDefault(); _description = descAttr != null ? descAttr.Description : ""; } // here's the really important part public override string Description { get { return _description; } set { _description = value; } } } </code></pre> <p>Then in your Global.asax in Application_Start or wherever you register your model metadata providers:</p> <pre><code>ModelMetadataProviders.Current = new CustomMetadataProvider(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload