Advertisement
philharvey

Umbraco dictionary metadata provider

Jan 17th, 2013
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. //WebBootManager.cs:
  2.  
  3. public override IBootManager Initialize()
  4. {
  5.     //...
  6.     // set metadata provider
  7.     ModelMetadataProviders.Current = new DictionaryMetadataProvider();
  8.     //...
  9. }
  10.  
  11. //DictionaryMetadataProvider.cs:
  12.  
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Web.Mvc;
  18. using umbraco;
  19. using System.ComponentModel.DataAnnotations;
  20.  
  21. namespace Umbraco.Web.Mvc
  22. {
  23.     /// <summary>
  24.     /// Model meta data provider that looks up display values and error messages from the Umbraco dictionary
  25.     /// </summary>
  26.     public class DictionaryMetadataProvider : DataAnnotationsModelMetadataProvider
  27.     {
  28.         protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
  29.         {
  30.             var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
  31.  
  32.             if (containerType != null && propertyName != null)
  33.             {
  34.                 string dictKey = containerType.Name + "." + propertyName;
  35.                 metadata.DisplayName = LookupDictionaryValue(dictKey, metadata.DisplayName);
  36.                 metadata.NullDisplayText = LookupDictionaryValue(dictKey + ".NullDisplayText", metadata.NullDisplayText);
  37.  
  38.                 foreach (var attribute in attributes)
  39.                 {
  40.                     if (attribute is ValidationAttribute)
  41.                     {
  42.                         string attributeName = attribute.GetType().Name;
  43.  
  44.                         if (attributeName.EndsWith("Attribute"))
  45.                             attributeName = attributeName.Substring(0, attributeName.Length - 9);
  46.  
  47.                         string attributeDictKey = containerType.Name + "." + propertyName + "." + attributeName;
  48.  
  49.                         var validationAttribute = (ValidationAttribute)attribute;
  50.  
  51.                         if (validationAttribute.ErrorMessageResourceName == null)
  52.                         {
  53.                             string errorMessage = LookupDictionaryValue(attributeDictKey, null);
  54.  
  55.                             if (errorMessage != null)
  56.                                 validationAttribute.ErrorMessage = errorMessage;
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             return metadata;
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Look up a value from the Umbraco dictionary
  67.         /// </summary>
  68.         /// <param name="dictKey">The dictionary item key</param>
  69.         /// <param name="defaultValue">The default value to return if the dictionary item was not found</param>
  70.         /// <returns>The dictionary item value, or defaultValue if item was not found in the dictionary</returns>
  71.         protected virtual string LookupDictionaryValue(string dictKey, string defaultValue)
  72.         {
  73.             string dictValue = library.GetDictionaryItem(dictKey);
  74.             return !string.IsNullOrEmpty(dictValue) ? dictValue : defaultValue;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement