Table of Contents

Class ModelStateDictionary

Namespace
System.Web.Http.ModelBinding
Assembly
System.Web.Http.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 dictionary.

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

The key.

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.

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.

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()

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 array. The array must have zero-based indexing.

arrayIndex int

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

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.

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.

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.

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

The value associated with the specified key.

Returns

bool

true if the object contains an element that has the specified key; otherwise, false.