Mercurial > altnet-hispano
annotate Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndConventionModelMetadataProvider.cs @ 155:23aaf98b8377
Generalizando editores en EditorDefault basando los textos en recursos
author | Nelo@Guinea.neluz.int |
---|---|
date | Wed, 03 Aug 2011 09:38:23 -0300 |
parents | c57ca21064f2 |
children | ea85bd893247 |
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 | |
19 protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) | |
20 { | |
21 _attributeList = new List<Attribute>(attributes); | |
22 | |
23 var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); | |
24 | |
25 if (propertyName != null) | |
26 { | |
27 if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName)) | |
28 modelMetadata.DisplayName = _resolverByConvention.GetDisplayName(propertyName); | |
29 | |
30 if (string.IsNullOrWhiteSpace(modelMetadata.Description)) | |
31 modelMetadata.Description = _resolverByConvention.GetDescription(propertyName); | |
32 | |
152
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
33 // Auto UIHint por property name. |
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
34 // TODO: Sería ideal chequear si existe el template antes de asignarlo, ¿cómo? |
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
35 if (string.IsNullOrWhiteSpace(modelMetadata.TemplateHint)) |
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
36 modelMetadata.TemplateHint = propertyName; |
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
37 |
85 | 38 var validators = _attributeList.OfType<ValidationAttribute>(); |
39 foreach (var validator in validators) | |
40 { | |
41 if (string.IsNullOrWhiteSpace(validator.ErrorMessage) && | |
42 string.IsNullOrWhiteSpace(validator.ErrorMessageResourceName)) | |
43 { | |
44 var resourceName = propertyName + validator.GetType().Name; | |
45 if (resourceName.EndsWith("Attribute")) | |
46 resourceName = resourceName.Substring(0, resourceName.Length - 9); | |
47 var resourceType = validator.ErrorMessageResourceType ?? _resolverByConvention.ResourceType; | |
48 var prop = resourceType.GetProperty(resourceName); | |
49 if (prop != null) | |
50 { | |
51 validator.ErrorMessageResourceType = resourceType; | |
52 validator.ErrorMessageResourceName = resourceName; | |
53 } | |
54 } | |
55 } | |
56 } | |
57 | |
58 return modelMetadata; | |
59 } | |
60 } | |
61 } |