Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an old post, but ran into this as well have have a slightly different solution than cfeduke. Figured I'd post it in case anyone else happens by here.</p> <p>At least in MVC 3, you don't need to define a custom metadata type, just the provider. Reading through the MVC source code, metadata isn't build from all possible attributes, just a few:</p> <p>DisplayAttribute provides:</p> <ul> <li>Description</li> <li>ShortDisplayName</li> <li>Watermark</li> <li>Order</li> </ul> <p>The DescriptionAttribute isn't checked for at all so if defined on your model, the metadata will reflect null. If you're having problems with metadata attributes not being set, check that the provider actually reads that.</p> <pre><code>class MyDataAnnotationsModelMetadataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata(IEnumerable&lt;Attribute&gt; attributes, Type containerType, Func&lt;object&gt; modelAccessor, Type modelType, string propertyName) { var metaData = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); // Description - The default provider reads the description from DisplayAttribute. // Here we check for a description attribute as well. This will overwrite anything // set before as we assume a Description attribute is more specific. DescriptionAttribute descriptionAttribute = attributes.OfType&lt;DescriptionAttribute&gt;().FirstOrDefault(); if (descriptionAttribute != null) { metaData.Description = descriptionAttribute.Description; } return metaData; } } </code></pre>
 

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