Mercurial > altnet-hispano
annotate Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndConventionModelMetadataProvider.cs @ 286:a8f7c41e3b47
#196: Patrocinadores, en el histórico de eventos.
author | juanjose.montesdeocaarbos |
---|---|
date | Mon, 02 Jan 2012 15:51:19 -0300 |
parents | ea85bd893247 |
children | 8b0c62c255cd |
rev | line source |
---|---|
85 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.ComponentModel.DataAnnotations; | |
4 using System.Linq; | |
5 using System.Web.Mvc; | |
6 | |
7 namespace AltNetHispano.Agendas.Web.CustomModelMetadataProvider | |
8 { | |
9 public class DataAnnotationAndConventionModelMetadataProvider : DataAnnotationsModelMetadataProvider | |
10 { | |
11 private readonly IResolverByConvention _resolverByConvention; | |
12 private List<Attribute> _attributeList; | |
13 | |
14 public DataAnnotationAndConventionModelMetadataProvider(IResolverByConvention resolverByConvention) | |
15 { | |
16 _resolverByConvention = resolverByConvention; | |
17 } | |
18 | |
167 | 19 protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, |
20 Func<object> modelAccessor, Type modelType, string propertyName) | |
85 | 21 { |
22 _attributeList = new List<Attribute>(attributes); | |
23 | |
24 var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); | |
25 | |
26 if (propertyName != null) | |
27 { | |
167 | 28 // Si no hay displayName asignado, lo buscamos entre los recursos |
85 | 29 if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName)) |
30 modelMetadata.DisplayName = _resolverByConvention.GetDisplayName(propertyName); | |
31 | |
167 | 32 // Si no hay description asignado, lo buscamos entre los recursos |
85 | 33 if (string.IsNullOrWhiteSpace(modelMetadata.Description)) |
34 modelMetadata.Description = _resolverByConvention.GetDescription(propertyName); | |
35 | |
167 | 36 // Auto UIHint por property name. |
37 // TODO: Sería ideal chequear si existe el template antes de asignarlo, ¿cómo? | |
38 if (string.IsNullOrWhiteSpace(modelMetadata.TemplateHint)) | |
39 modelMetadata.TemplateHint = propertyName; | |
152
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
40 |
167 | 41 // Para cada validador sin texto asignado, tomamos asignamos los valores para ser buscados |
42 // entre los recursos | |
85 | 43 var validators = _attributeList.OfType<ValidationAttribute>(); |
44 foreach (var validator in validators) | |
45 { | |
46 if (string.IsNullOrWhiteSpace(validator.ErrorMessage) && | |
47 string.IsNullOrWhiteSpace(validator.ErrorMessageResourceName)) | |
48 { | |
49 var resourceName = propertyName + validator.GetType().Name; | |
50 if (resourceName.EndsWith("Attribute")) | |
51 resourceName = resourceName.Substring(0, resourceName.Length - 9); | |
52 var resourceType = validator.ErrorMessageResourceType ?? _resolverByConvention.ResourceType; | |
53 var prop = resourceType.GetProperty(resourceName); | |
54 if (prop != null) | |
55 { | |
56 validator.ErrorMessageResourceType = resourceType; | |
57 validator.ErrorMessageResourceName = resourceName; | |
58 } | |
59 } | |
60 } | |
61 } | |
62 | |
63 return modelMetadata; | |
64 } | |
65 } | |
66 } |