ModelStateDictionary Class

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

Namespace
Microsoft.AspNetCore.Mvc.ModelBinding
Assemblies
  • Microsoft.AspNetCore.Mvc.Abstractions

Syntax

public class ModelStateDictionary : IReadOnlyDictionary<string, ModelStateEntry>, IReadOnlyCollection<KeyValuePair<string, ModelStateEntry>>, IEnumerable<KeyValuePair<string, ModelStateEntry>>, IEnumerable
class Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary

Constructors

ModelStateDictionary()

Initializes a new instance of the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary class.

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

Initializes a new instance of the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary class by using values that are copied from the specified <em>dictionary</em>.

Arguments:dictionary (Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary) – The Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary to copy values from.
public ModelStateDictionary(ModelStateDictionary dictionary)
ModelStateDictionary(System.Int32)

Initializes a new instance of the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary class.

public ModelStateDictionary(int maxAllowedErrors)

Methods

AddModelError(System.String, System.Exception, Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata)

Adds the specified <em>exception</em> to the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.Errors instance that is associated with the specified <em>key</em>.

Arguments:
public void AddModelError(string key, Exception exception, ModelMetadata metadata)
AddModelError(System.String, System.String)

Adds the specified <em>errorMessage</em> to the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.Errors instance that is associated with the specified <em>key</em>.

Arguments:
public void AddModelError(string key, string errorMessage)
Clear()

Removes all keys and values from ths instance of Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.

public void Clear()
ClearValidationState(System.String)

Clears Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary entries that match the key that is passed as parameter.

Arguments:key (System.String) – The key of Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary to clear.
public void ClearValidationState(string key)
ContainsKey(System.String)
Return type:System.Boolean
public bool ContainsKey(string key)
FindKeysWithPrefix(System.String)
Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.PrefixEnumerable
public ModelStateDictionary.PrefixEnumerable FindKeysWithPrefix(string prefix)
GetEnumerator()

Returns an enumerator that iterates through this instance of Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.

Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Enumerator
Returns:An Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Enumerator.
public ModelStateDictionary.Enumerator GetEnumerator()
GetFieldValidationState(System.String)

Returns the aggregate Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState for items starting with the specified <em>key</em>.

Arguments:key (System.String) – The key to look up model state errors for.
Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState
Returns:Returns Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Unvalidated if no entries are found for the specified key, Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid if at least one instance is found with one or more model state errors; Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid otherwise.
public ModelValidationState GetFieldValidationState(string key)
GetValidationState(System.String)

Returns Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState for the <em>key</em>.

Arguments:key (System.String) – The key to look up model state errors for.
Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState
Returns:Returns Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Unvalidated if no entry is found for the specified key, Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid if an instance is found with one or more model state errors; Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid otherwise.
public ModelValidationState GetValidationState(string key)
MarkFieldSkipped(System.String)

Marks the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.ValidationState for the entry with the specified <em>key</em> as Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Skipped.

Arguments:key (System.String) – The key of the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry to mark as skipped.
public void MarkFieldSkipped(string key)
MarkFieldValid(System.String)

Marks the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.ValidationState for the entry with the specified <em>key</em> as Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Valid.

Arguments:key (System.String) – The key of the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry to mark as valid.
public void MarkFieldValid(string key)
Merge(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary)

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

Arguments:dictionary (Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary) – The Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary to copy values from.
public void Merge(ModelStateDictionary dictionary)
Remove(System.String)

Removes the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry with the specified <em>key</em>.

Arguments:key (System.String) – The key.
Return type:System.Boolean
Returns:<code>true</code> if the element is successfully removed; otherwise <code>false</code>. This method also returns <code>false</code> if key was not found.
public bool Remove(string key)
SetModelValue(System.String, Microsoft.AspNetCore.Mvc.ModelBinding.ValueProviderResult)

Sets the value for the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry with the specified <em>key</em>.

Arguments:
public void SetModelValue(string key, ValueProviderResult valueProviderResult)
SetModelValue(System.String, System.Object, System.String)

Sets the of Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.RawValue and Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.AttemptedValue for the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry with the specified <em>key</em>.

Arguments:
public void SetModelValue(string key, object rawValue, string attemptedValue)
StartsWithPrefix(System.String, System.String)
Return type:System.Boolean
public static bool StartsWithPrefix(string prefix, string key)
GetEnumerator()
Return type:System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<System.String>>
IEnumerator<KeyValuePair<string, ModelStateEntry>> IEnumerable<KeyValuePair<string, ModelStateEntry>>.GetEnumerator()
GetEnumerator()
Return type:System.Collections.IEnumerator
IEnumerator IEnumerable.GetEnumerator()
TryAddModelError(System.String, System.Exception, Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata)

Attempts to add the specified <em>exception</em> to the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.Errors instance that is associated with the specified <em>key</em>. If the maximum number of allowed errors has already been recorded, records a Microsoft.AspNetCore.Mvc.ModelBinding.TooManyModelErrorsException exception instead.

Arguments:
Return type:

System.Boolean

Returns:

<code>True</code> if the given error was added, <code>false</code> if the error was ignored. See Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors.

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

Attempts to add the specified <em>errorMessage</em> to the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry.Errors instance that is associated with the specified <em>key</em>. If the maximum number of allowed errors has already been recorded, records a Microsoft.AspNetCore.Mvc.ModelBinding.TooManyModelErrorsException exception instead.

Arguments:
Return type:

System.Boolean

Returns:

<code>True</code> if the given error was added, <code>false</code> if the error was ignored. See Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors.

public bool TryAddModelError(string key, string errorMessage)
TryGetValue(System.String, out Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry)
Return type:System.Boolean
public bool TryGetValue(string key, out ModelStateEntry value)

Fields

DefaultMaxAllowedErrors()

The default value for Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors of <code>200</code>.

Return type:System.Int32
public static readonly int DefaultMaxAllowedErrors

Properties

Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Count
Return type:System.Int32
public int Count { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.ErrorCount

Gets the number of errors added to this instance of Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary via AddModelError or TryAddModelError.

Return type:System.Int32
public int ErrorCount { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.HasReachedMaxErrors

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

Return type:System.Boolean
public bool HasReachedMaxErrors { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.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.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Item[System.String]
Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry
public ModelStateEntry this[string key] { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Keys

Gets the key sequence.

Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.KeyEnumerable
public ModelStateDictionary.KeyEnumerable Keys { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.MaxAllowedErrors

Gets or sets the maximum allowed model state errors in this instance of Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary. Defaults to <code>200</code>.

Return type:System.Int32
public int MaxAllowedErrors { get; set; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Root

Root entry for the Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.

Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry
public ModelStateEntry Root { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Keys
Return type:System.Collections.Generic.IEnumerable<System.String>
IEnumerable<string> IReadOnlyDictionary<string, ModelStateEntry>.Keys { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Values
Return type:System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry>
IEnumerable<ModelStateEntry> IReadOnlyDictionary<string, ModelStateEntry>.Values { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.ValidationState
Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState
public ModelValidationState ValidationState { get; }
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.Values

Gets the value sequence.

Return type:Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary.ValueEnumerable
public ModelStateDictionary.ValueEnumerable Values { get; }