1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections;
\r
16 using System.Collections.Generic;
\r
17 using System.Diagnostics.CodeAnalysis;
\r
21 public class ModelStateDictionary : IDictionary<string, ModelState> {
\r
23 private readonly Dictionary<string, ModelState> _innerDictionary = new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);
\r
25 public ModelStateDictionary() {
\r
28 public ModelStateDictionary(ModelStateDictionary dictionary) {
\r
29 if (dictionary == null) {
\r
30 throw new ArgumentNullException("dictionary");
\r
33 foreach (var entry in dictionary) {
\r
34 _innerDictionary.Add(entry.Key, entry.Value);
\r
38 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
41 return _innerDictionary.Count;
\r
45 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
46 public bool IsReadOnly {
\r
48 return ((IDictionary<string, ModelState>)_innerDictionary).IsReadOnly;
\r
52 public bool IsValid {
\r
54 return Values.All(modelState => modelState.Errors.Count == 0);
\r
58 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
59 public ICollection<string> Keys {
\r
61 return _innerDictionary.Keys;
\r
65 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
66 public ModelState this[string key] {
\r
69 _innerDictionary.TryGetValue(key, out value);
\r
73 _innerDictionary[key] = value;
\r
77 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
78 public ICollection<ModelState> Values {
\r
80 return _innerDictionary.Values;
\r
84 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
85 public void Add(KeyValuePair<string, ModelState> item) {
\r
86 ((IDictionary<string, ModelState>)_innerDictionary).Add(item);
\r
89 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
90 public void Add(string key, ModelState value) {
\r
91 _innerDictionary.Add(key, value);
\r
94 public void AddModelError(string key, Exception exception) {
\r
95 GetModelStateForKey(key).Errors.Add(exception);
\r
98 public void AddModelError(string key, string errorMessage) {
\r
99 GetModelStateForKey(key).Errors.Add(errorMessage);
\r
102 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
103 public void Clear() {
\r
104 _innerDictionary.Clear();
\r
107 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
108 public bool Contains(KeyValuePair<string, ModelState> item) {
\r
109 return ((IDictionary<string, ModelState>)_innerDictionary).Contains(item);
\r
112 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
113 public bool ContainsKey(string key) {
\r
114 return _innerDictionary.ContainsKey(key);
\r
117 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
118 public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex) {
\r
119 ((IDictionary<string, ModelState>)_innerDictionary).CopyTo(array, arrayIndex);
\r
122 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
123 public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator() {
\r
124 return _innerDictionary.GetEnumerator();
\r
127 private ModelState GetModelStateForKey(string key) {
\r
129 throw new ArgumentNullException("key");
\r
132 ModelState modelState;
\r
133 if (!TryGetValue(key, out modelState)) {
\r
134 modelState = new ModelState();
\r
135 this[key] = modelState;
\r
141 public bool IsValidField(string key) {
\r
143 throw new ArgumentNullException("key");
\r
146 // if the key is not found in the dictionary, we just say that it's valid (since there are no errors)
\r
147 return DictionaryHelpers.FindKeysWithPrefix(this, key).All(entry => entry.Value.Errors.Count == 0);
\r
150 public void Merge(ModelStateDictionary dictionary) {
\r
151 if (dictionary == null) {
\r
155 foreach (var entry in dictionary) {
\r
156 this[entry.Key] = entry.Value;
\r
160 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
161 public bool Remove(KeyValuePair<string, ModelState> item) {
\r
162 return ((IDictionary<string, ModelState>)_innerDictionary).Remove(item);
\r
165 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
166 public bool Remove(string key) {
\r
167 return _innerDictionary.Remove(key);
\r
170 public void SetModelValue(string key, ValueProviderResult value) {
\r
171 GetModelStateForKey(key).Value = value;
\r
174 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
175 public bool TryGetValue(string key, out ModelState value) {
\r
176 return _innerDictionary.TryGetValue(key, out value);
\r
179 #region IEnumerable Members
\r
180 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
181 IEnumerator IEnumerable.GetEnumerator() {
\r
182 return ((IEnumerable)_innerDictionary).GetEnumerator();
\r