ModelStateDictionary Class

Summary

Represents the state of an attempt to bind values from an HTTP Request to an action method, which includes validation information.

Syntax

public class ModelStateDictionary : IDictionary<string, ModelState>, ICollection<KeyValuePair<string, ModelState>>, IEnumerable<KeyValuePair<string, ModelState>>, IEnumerable

GitHub

View on GitHub

class Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary

Fields

DefaultMaxAllowedErrors()

The default value for [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) of <c>200</c>.

public static readonly int DefaultMaxAllowedErrors

Constructors

Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.ModelStateDictionary()

Initializes a new instance of the [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) class.

public ModelStateDictionary()
ModelStateDictionary(Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary)

Initializes a new instance of the [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) class by using values that are copied from the specified <paramref name=”dictionary” />.

Arguments:
public ModelStateDictionary(ModelStateDictionary dictionary)
ModelStateDictionary(System.Int32)

Initializes a new instance of the [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) class.

Arguments:
  • maxAllowedErrors (System.Int32) –
public ModelStateDictionary(int maxAllowedErrors)

Properties

Count()
Return type:System.Int32
public int Count { get; }
ErrorCount()

Gets the number of errors added to this instance of [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) via [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.AddModelError(System.String,System.Exception)](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) or [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.TryAddModelError(System.String,System.Exception)](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml).

Return type:System.Int32
public int ErrorCount { get; }
HasReachedMaxErrors()

Gets a value indicating whether or not the maximum number of errors have been recorded.

Return type:System.Boolean
public bool HasReachedMaxErrors { get; }
IsReadOnly()
Return type:System.Boolean
public bool IsReadOnly { get; }
IsValid()

Gets a value that indicates whether any model state values in this model state dictionary is invalid or not validated.

Return type:System.Boolean
public bool IsValid { get; }
Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.Item[System.String]
Return type:Microsoft.AspNet.Mvc.ModelBinding.ModelState
public ModelState this[string key] { get; set; }
Keys()
Return type:System.Collections.Generic.ICollection{System.String}
public ICollection<string> Keys { get; }
MaxAllowedErrors()

Gets or sets the maximum allowed model state errors in this instance of [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml). Defaults to <c>200</c>.

Return type:System.Int32
public int MaxAllowedErrors { get; set; }
ValidationState()
Return type:Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState
public ModelValidationState ValidationState { get; }
Values()
Return type:System.Collections.Generic.ICollection{Microsoft.AspNet.Mvc.ModelBinding.ModelState}
public ICollection<ModelState> Values { get; }

Methods

Add(System.Collections.Generic.KeyValuePair<System.String, Microsoft.AspNet.Mvc.ModelBinding.ModelState>)
Arguments:
  • item (System.Collections.Generic.KeyValuePair{System.String,Microsoft.AspNet.Mvc.ModelBinding.ModelState}) –
public void Add(KeyValuePair<string, ModelState> item)
Add(System.String, Microsoft.AspNet.Mvc.ModelBinding.ModelState)
Arguments:
public void Add(string key, ModelState value)
AddModelError(System.String, System.Exception)

Adds the specified <paramref name=”exception” /> to the [Microsoft.AspNet.Mvc.ModelBinding.ModelState.Errors](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) instance that is associated with the specified <paramref name=”key” />.

Arguments:
  • key (System.String) – The key of the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) to add errors to.
  • exception (System.Exception) – The [System.Exception]() to add.
public void AddModelError(string key, Exception exception)
AddModelError(System.String, System.String)

Adds the specified <paramref name=”errorMessage” /> to the [Microsoft.AspNet.Mvc.ModelBinding.ModelState.Errors](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) instance that is associated with the specified <paramref name=”key” />.

Arguments:
  • key (System.String) – The key of the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) to add errors to.
  • errorMessage (System.String) – The error message to add.
public void AddModelError(string key, string errorMessage)
Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.Clear()
public void Clear()
ClearValidationState(System.String)

Clears [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) entries that match the key that is passed as parameter.

Arguments:
  • key (System.String) – The key of [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml) to clear.
public void ClearValidationState(string key)
Contains(System.Collections.Generic.KeyValuePair<System.String, Microsoft.AspNet.Mvc.ModelBinding.ModelState>)
Arguments:
  • item (System.Collections.Generic.KeyValuePair{System.String,Microsoft.AspNet.Mvc.ModelBinding.ModelState}) –
Return type:

System.Boolean

public bool Contains(KeyValuePair<string, ModelState> item)
ContainsKey(System.String)
Arguments:
  • key (System.String) –
Return type:

System.Boolean

public bool ContainsKey(string key)
CopyTo(System.Collections.Generic.KeyValuePair<System.String, Microsoft.AspNet.Mvc.ModelBinding.ModelState>[], System.Int32)
Arguments:
  • array (System.Collections.Generic.KeyValuePair{System.String,Microsoft.AspNet.Mvc.ModelBinding.ModelState}[]) –
  • arrayIndex (System.Int32) –
public void CopyTo(KeyValuePair<string, ModelState>[] array, int arrayIndex)
FindKeysWithPrefix(System.String)
Arguments:
  • prefix (System.String) –
Return type:

System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{System.String,Microsoft.AspNet.Mvc.ModelBinding.ModelState}}

public IEnumerable<KeyValuePair<string, ModelState>> FindKeysWithPrefix(string prefix)
Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.GetEnumerator()
Return type:System.Collections.Generic.IEnumerator{System.Collections.Generic.KeyValuePair{System.String,Microsoft.AspNet.Mvc.ModelBinding.ModelState}}
public IEnumerator<KeyValuePair<string, ModelState>> GetEnumerator()
GetFieldValidationState(System.String)

Returns the aggregate [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) for items starting with the specified <paramref name=”key” />.

Arguments:
  • key (System.String) – The key to look up model state errors for.
Return type:

Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState

Returns:

Returns [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Unvalidated](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) if no entries are found for the specified key, [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Invalid](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) if at least one instance is found with one or more model state errors; [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Valid](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) otherwise.

public ModelValidationState GetFieldValidationState(string key)
GetValidationState(System.String)

Returns [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) for the <paramref name=”key” />.

Arguments:
  • key (System.String) – The key to look up model state errors for.
Return type:

Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState

Returns:

Returns [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Unvalidated](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) if no entry is found for the specified key, [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Invalid](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) if an instance is found with one or more model state errors; [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Valid](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml) otherwise.

public ModelValidationState GetValidationState(string key)
MarkFieldSkipped(System.String)

Marks the [Microsoft.AspNet.Mvc.ModelBinding.ModelState.ValidationState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) for the entry with the specified <paramref name=”key” /> as [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Skipped](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml).

Arguments:
  • key (System.String) – The key of the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) to mark as skipped.
public void MarkFieldSkipped(string key)
MarkFieldValid(System.String)

Marks the [Microsoft.AspNet.Mvc.ModelBinding.ModelState.ValidationState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) for the entry with the specified <paramref name=”key” /> as [Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.Valid](Microsoft.AspNet.Mvc.ModelBinding.ModelValidationState.yml).

Arguments:
  • key (System.String) – The key of the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) to mark as valid.
public void MarkFieldValid(string key)
Merge(Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary)

Copies the values from the specified <paramref name=”dictionary” /> into this instance, overwriting existing values if keys are the same.

Arguments:
public void Merge(ModelStateDictionary dictionary)
Remove(System.Collections.Generic.KeyValuePair<System.String, Microsoft.AspNet.Mvc.ModelBinding.ModelState>)
Arguments:
  • item (System.Collections.Generic.KeyValuePair{System.String,Microsoft.AspNet.Mvc.ModelBinding.ModelState}) –
Return type:

System.Boolean

public bool Remove(KeyValuePair<string, ModelState> item)
Remove(System.String)
Arguments:
  • key (System.String) –
Return type:

System.Boolean

public bool Remove(string key)
SetModelValue(System.String, Microsoft.AspNet.Mvc.ModelBinding.ValueProviderResult)

Sets the value for the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) with the specified <paramref name=”key” /> to the specified <paramref name=”value” />.

Arguments:
public void SetModelValue(string key, ValueProviderResult value)
Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.System.Collections.IEnumerable.GetEnumerator()
Return type:System.Collections.IEnumerator
IEnumerator IEnumerable.GetEnumerator()
TryAddModelError(System.String, System.Exception)

Attempts to add the specified <paramref name=”exception” /> to the [Microsoft.AspNet.Mvc.ModelBinding.ModelState.Errors](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) instance that is associated with the specified <paramref name=”key” />. If the maximum number of allowed errors has already been recorded, records a [Microsoft.AspNet.Mvc.ModelBinding.TooManyModelErrorsException](Microsoft.AspNet.Mvc.ModelBinding.TooManyModelErrorsException.yml) exception instead.

Arguments:
  • key (System.String) – The key of the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) to add errors to.
  • exception (System.Exception) – The [System.Exception]() to add.
Return type:

System.Boolean

Returns:

<c>True</c> if the given error was added, <c>false</c> if the error was ignored. See [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml).

public bool TryAddModelError(string key, Exception exception)
TryAddModelError(System.String, System.String)

Attempts to add the specified <paramref name=”errorMessage” /> to the [Microsoft.AspNet.Mvc.ModelBinding.ModelState.Errors](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) instance that is associated with the specified <paramref name=”key” />. If the maximum number of allowed errors has already been recorded, records a [Microsoft.AspNet.Mvc.ModelBinding.TooManyModelErrorsException](Microsoft.AspNet.Mvc.ModelBinding.TooManyModelErrorsException.yml) exception instead.

Arguments:
  • key (System.String) – The key of the [Microsoft.AspNet.Mvc.ModelBinding.ModelState](Microsoft.AspNet.Mvc.ModelBinding.ModelState.yml) to add errors to.
  • errorMessage (System.String) – The error message to add.
Return type:

System.Boolean

Returns:

<c>True</c> if the given error was added, <c>false</c> if the error was ignored. See [Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors](Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary.yml).

public bool TryAddModelError(string key, string errorMessage)
TryGetValue(System.String, out Microsoft.AspNet.Mvc.ModelBinding.ModelState)
Arguments:
Return type:

System.Boolean

public bool TryGetValue(string key, out ModelState value)