Table of Contents

Class ModelStateDictionary

Namespace
System.Web.Mvc
Assembly
System.Web.Mvc.dll

Represents the state of an attempt to bind a posted form to an action method, which includes validation information.

public class ModelStateDictionary : IDictionary<string, ModelState>, ICollection<KeyValuePair<string, ModelState>>, IEnumerable<KeyValuePair<string, ModelState>>, IEnumerable
Inheritance
ModelStateDictionary
Implements
Inherited Members

Constructors

ModelStateDictionary()

Initializes a new instance of the ModelStateDictionary class.

public ModelStateDictionary()

ModelStateDictionary(ModelStateDictionary)

Initializes a new instance of the ModelStateDictionary class by using values that are copied from the specified model-state dictionary.

public ModelStateDictionary(ModelStateDictionary dictionary)

Parameters

dictionary ModelStateDictionary

The model-state dictionary.

Exceptions

ArgumentNullException

The dictionary parameter is null.

Properties

Count

Gets the number of key/value pairs in the collection.

public int Count { get; }

Property Value

int

The number of key/value pairs in the collection.

IsReadOnly

Gets a value that indicates whether the collection is read-only.

public bool IsReadOnly { get; }

Property Value

bool

true if the collection is read-only; otherwise, false.

IsValid

Gets a value that indicates whether this instance of the model-state dictionary is valid.

public bool IsValid { get; }

Property Value

bool

true if this instance is valid; otherwise, false.

this[string]

Gets or sets the value that is associated with the specified key.

public ModelState this[string key] { get; set; }

Parameters

key string

Property Value

ModelState

The model state item.

Keys

Gets a collection that contains the keys in the dictionary.

public ICollection<string> Keys { get; }

Property Value

ICollection<string>

A collection that contains the keys of the model-state dictionary.

Values

Gets a collection that contains the values in the dictionary.

public ICollection<ModelState> Values { get; }

Property Value

ICollection<ModelState>

A collection that contains the values of the model-state dictionary.

Methods

Add(KeyValuePair<string, ModelState>)

Adds the specified item to the model-state dictionary.

public void Add(KeyValuePair<string, ModelState> item)

Parameters

item KeyValuePair<string, ModelState>

The object to add to the model-state dictionary.

Exceptions

NotSupportedException

The model-state dictionary is read-only.

Add(string, ModelState)

Adds an element that has the specified key and value to the model-state dictionary.

public void Add(string key, ModelState value)

Parameters

key string

The key of the element to add.

value ModelState

The value of the element to add.

Exceptions

NotSupportedException

The model-state dictionary is read-only.

ArgumentNullException

key is null.

ArgumentException

An element that has the specified key already occurs in the model-state dictionary.

AddModelError(string, Exception)

Adds the specified model error to the errors collection for the model-state dictionary that is associated with the specified key.

public void AddModelError(string key, Exception exception)

Parameters

key string

The key.

exception Exception

The exception.

AddModelError(string, string)

Adds the specified error message to the errors collection for the model-state dictionary that is associated with the specified key.

public void AddModelError(string key, string errorMessage)

Parameters

key string

The key.

errorMessage string

The error message.

Clear()

Removes all items from the model-state dictionary.

public void Clear()

Exceptions

NotSupportedException

The model-state dictionary is read-only.

Contains(KeyValuePair<string, ModelState>)

Determines whether the model-state dictionary contains a specific value.

public bool Contains(KeyValuePair<string, ModelState> item)

Parameters

item KeyValuePair<string, ModelState>

The object to locate in the model-state dictionary.

Returns

bool

true if item is found in the model-state dictionary; otherwise, false.

ContainsKey(string)

Determines whether the model-state dictionary contains the specified key.

public bool ContainsKey(string key)

Parameters

key string

The key to locate in the model-state dictionary.

Returns

bool

true if the model-state dictionary contains the specified key; otherwise, false.

CopyTo(KeyValuePair<string, ModelState>[], int)

Copies the elements of the model-state dictionary to an array, starting at a specified index.

public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex)

Parameters

array KeyValuePair<string, ModelState>[]

The one-dimensional array that is the destination of the elements copied from the ICollection<T> object. The array must have zero-based indexing.

arrayIndex int

The zero-based index in array at which copying starts.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

arrayIndex is less than 0.

ArgumentException

array is multidimensional.-or- arrayIndex is equal to or greater than the length of array.-or- The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array.-or- Type T cannot be cast automatically to the type of the destination array.

GetEnumerator()

Returns an enumerator that can be used to iterate through the collection.

public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<string, ModelState>>

An enumerator that can be used to iterate through the collection.

IsValidField(string)

Determines whether there are any ModelError objects that are associated with or prefixed with the specified key.

public bool IsValidField(string key)

Parameters

key string

The key.

Returns

bool

true if the model-state dictionary contains a value that is associated with the specified key; otherwise, false.

Exceptions

ArgumentNullException

The key parameter is null.

Merge(ModelStateDictionary)

Copies the values from the specified ModelStateDictionary object into this dictionary, overwriting existing values if keys are the same.

public void Merge(ModelStateDictionary dictionary)

Parameters

dictionary ModelStateDictionary

The dictionary.

Remove(KeyValuePair<string, ModelState>)

Removes the first occurrence of the specified object from the model-state dictionary.

public bool Remove(KeyValuePair<string, ModelState> item)

Parameters

item KeyValuePair<string, ModelState>

The object to remove from the model-state dictionary.

Returns

bool

true if item was successfully removed the model-state dictionary; otherwise, false. This method also returns false if item is not found in the model-state dictionary.

Exceptions

NotSupportedException

The model-state dictionary is read-only.

Remove(string)

Removes the element that has the specified key from the model-state dictionary.

public bool Remove(string key)

Parameters

key string

The key of the element to remove.

Returns

bool

true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the model-state dictionary.

Exceptions

NotSupportedException

The model-state dictionary is read-only.

ArgumentNullException

key is null.

SetModelValue(string, ValueProviderResult)

Sets the value for the specified key by using the specified value provider dictionary.

public void SetModelValue(string key, ValueProviderResult value)

Parameters

key string

The key.

value ValueProviderResult

The value.

TryGetValue(string, out ModelState)

Attempts to gets the value that is associated with the specified key.

public bool TryGetValue(string key, out ModelState value)

Parameters

key string

The key of the value to get.

value ModelState

When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.

Returns

bool

true if the object that implements IDictionary<TKey, TValue> contains an element that has the specified key; otherwise, false.

Exceptions

ArgumentNullException

key is null.