forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAnnotationsModelMetadataProvider.cs
More file actions
148 lines (129 loc) · 6.86 KB
/
DataAnnotationsModelMetadataProvider.cs
File metadata and controls
148 lines (129 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace System.Web.Mvc
{
public class DataAnnotationsModelMetadataProvider : AssociatedMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
List<Attribute> attributeList = new List<Attribute>(attributes);
DisplayColumnAttribute displayColumnAttribute = attributeList.OfType<DisplayColumnAttribute>().FirstOrDefault();
DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
// Do [HiddenInput] before [UIHint], so you can override the template hint
HiddenInputAttribute hiddenInputAttribute = attributeList.OfType<HiddenInputAttribute>().FirstOrDefault();
if (hiddenInputAttribute != null)
{
result.TemplateHint = "HiddenInput";
result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
}
// We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
IEnumerable<UIHintAttribute> uiHintAttributes = attributeList.OfType<UIHintAttribute>();
UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));
if (uiHintAttribute != null)
{
result.TemplateHint = uiHintAttribute.UIHint;
}
EditableAttribute editable = attributes.OfType<EditableAttribute>().FirstOrDefault();
if (editable != null)
{
result.IsReadOnly = !editable.AllowEdit;
}
else
{
ReadOnlyAttribute readOnlyAttribute = attributeList.OfType<ReadOnlyAttribute>().FirstOrDefault();
if (readOnlyAttribute != null)
{
result.IsReadOnly = readOnlyAttribute.IsReadOnly;
}
}
DataTypeAttribute dataTypeAttribute = attributeList.OfType<DataTypeAttribute>().FirstOrDefault();
DisplayFormatAttribute displayFormatAttribute = attributeList.OfType<DisplayFormatAttribute>().FirstOrDefault();
SetFromDataTypeAndDisplayAttributes(result, dataTypeAttribute, displayFormatAttribute);
ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType<ScaffoldColumnAttribute>().FirstOrDefault();
if (scaffoldColumnAttribute != null)
{
result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
}
DisplayAttribute display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
string name = null;
if (display != null)
{
result.Description = display.GetDescription();
result.ShortDisplayName = display.GetShortName();
result.Watermark = display.GetPrompt();
result.Order = display.GetOrder() ?? ModelMetadata.DefaultOrder;
name = display.GetName();
}
if (name != null)
{
result.DisplayName = name;
}
else
{
DisplayNameAttribute displayNameAttribute = attributeList.OfType<DisplayNameAttribute>().FirstOrDefault();
if (displayNameAttribute != null)
{
result.DisplayName = displayNameAttribute.DisplayName;
}
}
RequiredAttribute requiredAttribute = attributeList.OfType<RequiredAttribute>().FirstOrDefault();
if (requiredAttribute != null)
{
result.IsRequired = true;
}
return result;
}
private static void SetFromDataTypeAndDisplayAttributes(DataAnnotationsModelMetadata result,
DataTypeAttribute dataTypeAttribute, DisplayFormatAttribute displayFormatAttribute)
{
if (dataTypeAttribute != null)
{
result.DataTypeName = dataTypeAttribute.ToDataTypeName();
}
if (displayFormatAttribute == null && dataTypeAttribute != null)
{
displayFormatAttribute = dataTypeAttribute.DisplayFormat;
// If DisplayFormat value was non-null and this [DataType] is of a subclass, assume the [DataType]
// constructor used the protected DisplayFormat setter to override its default. Note deriving from
// [DataType] but preserving DataFormatString and ApplyFormatInEditMode results in
// HasNonDefaultEditFormat==true.
if (displayFormatAttribute != null && dataTypeAttribute.GetType() != typeof(DataTypeAttribute))
{
result.HasNonDefaultEditFormat = true;
}
}
else if (displayFormatAttribute != null)
{
result.HasNonDefaultEditFormat = true;
}
if (displayFormatAttribute != null)
{
result.NullDisplayText = displayFormatAttribute.NullDisplayText;
result.DisplayFormatString = displayFormatAttribute.DataFormatString;
result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
result.HtmlEncode = displayFormatAttribute.HtmlEncode;
if (displayFormatAttribute.ApplyFormatInEditMode)
{
result.EditFormatString = displayFormatAttribute.DataFormatString;
}
if (!displayFormatAttribute.HtmlEncode && String.IsNullOrWhiteSpace(result.DataTypeName))
{
result.DataTypeName = DataTypeUtil.HtmlTypeName;
}
// Regardless of HasNonDefaultEditFormat calculation above, treat missing EditFormatString as the
// default. Note the corner case of a [DataType] subclass overriding a non-empty default to apply a
// [DisplayFormat] lacking DataFormatString or with ApplyFormatInEditMode==false results in
// HasNonDefaultEditFormat==false.
if (String.IsNullOrEmpty(result.EditFormatString))
{
result.HasNonDefaultEditFormat = false;
}
}
}
}
}