forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedDataAnnotationsModelMetadata.cs
More file actions
83 lines (71 loc) · 3.29 KB
/
CachedDataAnnotationsModelMetadata.cs
File metadata and controls
83 lines (71 loc) · 3.29 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
// 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;
namespace System.Web.Http.Metadata.Providers
{
// REVIEW: No access to HiddenInputAttribute
public class CachedDataAnnotationsModelMetadata : CachedModelMetadata<CachedDataAnnotationsMetadataAttributes>
{
public CachedDataAnnotationsModelMetadata(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
: base(prototype, modelAccessor)
{
}
public CachedDataAnnotationsModelMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Type modelType, string propertyName, IEnumerable<Attribute> attributes)
: base(provider, containerType, modelType, propertyName, new CachedDataAnnotationsMetadataAttributes(attributes))
{
}
protected override bool ComputeConvertEmptyStringToNull()
{
return PrototypeCache.DisplayFormat != null
? PrototypeCache.DisplayFormat.ConvertEmptyStringToNull
: base.ComputeConvertEmptyStringToNull();
}
protected override string ComputeDescription()
{
return PrototypeCache.Display != null
? PrototypeCache.Display.GetDescription()
: base.ComputeDescription();
}
protected override bool ComputeIsReadOnly()
{
if (PrototypeCache.Editable != null)
{
return !PrototypeCache.Editable.AllowEdit;
}
if (PrototypeCache.ReadOnly != null)
{
return PrototypeCache.ReadOnly.IsReadOnly;
}
return base.ComputeIsReadOnly();
}
public override string GetDisplayName()
{
// DisplayName could be provided by either the DisplayAttribute, or DisplayNameAttribute. If neither of
// those supply a name, then we fall back to the property name (in base.GetDisplayName()).
//
// DisplayName has lower precedence than Display.Name, for consistency with MVC.
// DisplayAttribute doesn't require you to set a name, so this could be null.
if (PrototypeCache.Display != null)
{
string name = PrototypeCache.Display.GetName();
if (name != null)
{
return name;
}
}
// It's also possible for DisplayNameAttribute to be used without setting a name. If a user does that, then DisplayName will
// return the empty string - but for consistency with MVC we allow it. We do fallback to the property name in the (unlikely)
// scenario that the user sets null as the DisplayName, again, for consistency with MVC.
if (PrototypeCache.DisplayName != null)
{
string name = PrototypeCache.DisplayName.DisplayName;
if (name != null)
{
return name;
}
}
// If neither attribute specifies a name, we'll fall back to the property name.
return base.GetDisplayName();
}
}
}