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
20 public class ModelStateDictionary : IDictionary<string, ModelState> {
\r
22 private readonly Dictionary<string, ModelState> _innerDictionary = new Dictionary<string, ModelState>(StringComparer.OrdinalIgnoreCase);
\r
24 public ModelStateDictionary() {
\r
27 public ModelStateDictionary(ModelStateDictionary dictionary) {
\r
28 if (dictionary == null) {
\r
29 throw new ArgumentNullException("dictionary");
\r
32 foreach (var entry in dictionary) {
\r
33 _innerDictionary.Add(entry.Key, entry.Value);
\r
39 return _innerDictionary.Count;
\r
43 public bool IsReadOnly {
\r
45 return ((IDictionary<string, ModelState>)_innerDictionary).IsReadOnly;
\r
49 public bool IsValid {
\r
51 return Values.All(modelState => modelState.Errors.Count == 0);
\r
55 public ICollection<string> Keys {
\r
57 return _innerDictionary.Keys;
\r
61 public ModelState this[string key] {
\r
64 _innerDictionary.TryGetValue(key, out value);
\r
68 _innerDictionary[key] = value;
\r
72 public ICollection<ModelState> Values {
\r
74 return _innerDictionary.Values;
\r
78 public void Add(KeyValuePair<string, ModelState> item) {
\r
79 ((IDictionary<string, ModelState>)_innerDictionary).Add(item);
\r
82 public void Add(string key, ModelState value) {
\r
83 _innerDictionary.Add(key, value);
\r
86 public void AddModelError(string key, Exception exception) {
\r
87 GetModelStateForKey(key).Errors.Add(exception);
\r
90 public void AddModelError(string key, string errorMessage) {
\r
91 GetModelStateForKey(key).Errors.Add(errorMessage);
\r
94 public void Clear() {
\r
95 _innerDictionary.Clear();
\r
98 public bool Contains(KeyValuePair<string, ModelState> item) {
\r
99 return ((IDictionary<string, ModelState>)_innerDictionary).Contains(item);
\r
102 public bool ContainsKey(string key) {
\r
103 return _innerDictionary.ContainsKey(key);
\r
106 public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex) {
\r
107 ((IDictionary<string, ModelState>)_innerDictionary).CopyTo(array, arrayIndex);
\r
110 public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator() {
\r
111 return _innerDictionary.GetEnumerator();
\r
114 private ModelState GetModelStateForKey(string key) {
\r
116 throw new ArgumentNullException("key");
\r
119 ModelState modelState;
\r
120 if (!TryGetValue(key, out modelState)) {
\r
121 modelState = new ModelState();
\r
122 this[key] = modelState;
\r
128 public bool IsValidField(string key) {
\r
130 throw new ArgumentNullException("key");
\r
133 // if the key is not found in the dictionary, we just say that it's valid (since there are no errors)
\r
134 return DictionaryHelpers.FindKeysWithPrefix(this, key).All(entry => entry.Value.Errors.Count == 0);
\r
137 public void Merge(ModelStateDictionary dictionary) {
\r
138 if (dictionary == null) {
\r
142 foreach (var entry in dictionary) {
\r
143 this[entry.Key] = entry.Value;
\r
147 public bool Remove(KeyValuePair<string, ModelState> item) {
\r
148 return ((IDictionary<string, ModelState>)_innerDictionary).Remove(item);
\r
151 public bool Remove(string key) {
\r
152 return _innerDictionary.Remove(key);
\r
155 public void SetModelValue(string key, ValueProviderResult value) {
\r
156 GetModelStateForKey(key).Value = value;
\r
159 public bool TryGetValue(string key, out ModelState value) {
\r
160 return _innerDictionary.TryGetValue(key, out value);
\r
163 #region IEnumerable Members
\r
164 IEnumerator IEnumerable.GetEnumerator() {
\r
165 return ((IEnumerable)_innerDictionary).GetEnumerator();
\r