Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ModelBindingContext.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Diagnostics.CodeAnalysis;\r
17     using System.Linq;\r
18     using System.Web.Mvc.Resources;\r
19 \r
20     public class ModelBindingContext {\r
21 \r
22         private static readonly Predicate<string> _defaultPropertyFilter = _ => true;\r
23 \r
24         private string _modelName;\r
25         private ModelStateDictionary _modelState;\r
26         private Predicate<string> _propertyFilter;\r
27         private Dictionary<string, ModelMetadata> _propertyMetadata;\r
28 \r
29         public ModelBindingContext()\r
30             : this(null) {\r
31         }\r
32 \r
33         // copies certain values that won't change between parent and child objects,\r
34         // e.g. ValueProvider, ModelState\r
35         public ModelBindingContext(ModelBindingContext bindingContext) {\r
36             if (bindingContext != null) {\r
37                 ModelState = bindingContext.ModelState;\r
38                 ValueProvider = bindingContext.ValueProvider;\r
39             }\r
40         }\r
41 \r
42         public bool FallbackToEmptyPrefix {\r
43             get;\r
44             set;\r
45         }\r
46 \r
47         [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value", Justification = "Cannot remove setter as that's a breaking change")]\r
48         public object Model {\r
49             get {\r
50                 return ModelMetadata.Model;\r
51             }\r
52             set {\r
53                 throw new InvalidOperationException(MvcResources.ModelMetadata_PropertyNotSettable);\r
54             }\r
55         }\r
56 \r
57         public ModelMetadata ModelMetadata {\r
58             get;\r
59             set;\r
60         }\r
61 \r
62         public string ModelName {\r
63             get {\r
64                 if (_modelName == null) {\r
65                     _modelName = String.Empty;\r
66                 }\r
67                 return _modelName;\r
68             }\r
69             set {\r
70                 _modelName = value;\r
71             }\r
72         }\r
73 \r
74         [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The containing type is mutable.")]\r
75         public ModelStateDictionary ModelState {\r
76             get {\r
77                 if (_modelState == null) {\r
78                     _modelState = new ModelStateDictionary();\r
79                 }\r
80                 return _modelState;\r
81             }\r
82             set {\r
83                 _modelState = value;\r
84             }\r
85         }\r
86 \r
87         [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value", Justification = "Cannot remove setter as that's a breaking change")]\r
88         public Type ModelType {\r
89             get {\r
90                 return ModelMetadata.ModelType;\r
91             }\r
92             set {\r
93                 throw new InvalidOperationException(MvcResources.ModelMetadata_PropertyNotSettable);\r
94             }\r
95         }\r
96 \r
97         public Predicate<string> PropertyFilter {\r
98             get {\r
99                 if (_propertyFilter == null) {\r
100                     _propertyFilter = _defaultPropertyFilter;\r
101                 }\r
102                 return _propertyFilter;\r
103             }\r
104             set {\r
105                 _propertyFilter = value;\r
106             }\r
107         }\r
108 \r
109         public IDictionary<string, ModelMetadata> PropertyMetadata {\r
110             get {\r
111                 if (_propertyMetadata == null) {\r
112                     _propertyMetadata = ModelMetadata.Properties.ToDictionary(m => m.PropertyName, StringComparer.OrdinalIgnoreCase);\r
113                 }\r
114 \r
115                 return _propertyMetadata;\r
116             }\r
117         }\r
118 \r
119         public IValueProvider ValueProvider {\r
120             get;\r
121             set;\r
122         }\r
123 \r
124     }\r
125 }\r