Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

ModelStateDictionary Class

Definition

Namespace:
Microsoft.AspNetCore.Mvc.ModelBinding
Assembly:
Microsoft.AspNetCore.Mvc.Abstractions.dll
Package:
Microsoft.AspNetCore.Mvc.Abstractions v1.0.0
Package:
Microsoft.AspNetCore.Mvc.Abstractions v1.1.0
Package:
Microsoft.AspNetCore.App.Ref v10.0.0
Package:
Microsoft.AspNetCore.App.Ref v11.0.0-preview.4.26230.115
Package:
Microsoft.AspNetCore.Mvc.Abstractions v2.0.0
Package:
Microsoft.AspNetCore.Mvc.Abstractions v2.1.0
Package:
Microsoft.AspNetCore.Mvc.Abstractions v2.2.0
Package:
Microsoft.AspNetCore.App.Ref v3.0.1
Package:
Microsoft.AspNetCore.App.Ref v3.1.10
Package:
Microsoft.AspNetCore.App.Ref v5.0.0
Package:
Microsoft.AspNetCore.App.Ref v6.0.36
Package:
Microsoft.AspNetCore.App.Ref v7.0.5
Package:
Microsoft.AspNetCore.App.Ref v8.0.19
Package:
Microsoft.AspNetCore.App.Ref v9.0.8
Source:
ModelStateDictionary.cs
Source:
ModelStateDictionary.cs
Source:
ModelStateDictionary.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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

public ref class ModelStateDictionary : System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<System::String ^, Microsoft::AspNetCore::Mvc::ModelBinding::ModelStateEntry ^>>, System::Collections::Generic::IReadOnlyCollection<System::Collections::Generic::KeyValuePair<System::String ^, Microsoft::AspNetCore::Mvc::ModelBinding::ModelStateEntry ^>>, System::Collections::Generic::IReadOnlyDictionary<System::String ^, Microsoft::AspNetCore::Mvc::ModelBinding::ModelStateEntry ^>
public class ModelStateDictionary : System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<string,Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry>>, System.Collections.Generic.IReadOnlyDictionary<string,Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateEntry>
type ModelStateDictionary = class
 interface IReadOnlyDictionary<string, ModelStateEntry>
 interface seq<KeyValuePair<string, ModelStateEntry>>
 interface IEnumerable
 interface IReadOnlyCollection<KeyValuePair<string, ModelStateEntry>>
type ModelStateDictionary = class
 interface seq<KeyValuePair<string, ModelStateEntry>>
 interface IEnumerable
 interface IReadOnlyCollection<KeyValuePair<string, ModelStateEntry>>
 interface IReadOnlyDictionary<string, ModelStateEntry>
Public Class ModelStateDictionary
Implements IEnumerable(Of KeyValuePair(Of String, ModelStateEntry)), IReadOnlyCollection(Of KeyValuePair(Of String, ModelStateEntry)), IReadOnlyDictionary(Of String, ModelStateEntry)
Inheritance
ModelStateDictionary
Implements

Constructors

Name Description
ModelStateDictionary()

Initializes a new instance of the ModelStateDictionary class.

ModelStateDictionary(Int32)

Initializes a new instance of the ModelStateDictionary class.

ModelStateDictionary(ModelStateDictionary)

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

Fields

Name Description
DefaultMaxAllowedErrors

The default value for MaxAllowedErrors of 200.

Properties

Name Description
Count

Gets the number of elements in the collection.

ErrorCount

Gets the number of errors added to this instance of ModelStateDictionary via AddModelError or TryAddModelError.

HasReachedMaxErrors

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

IsValid

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

Item[String]

Gets the element that has the specified key in the read-only dictionary.

Keys

Gets the key sequence.

MaxAllowedErrors

Gets or sets the maximum allowed model state errors in this instance of ModelStateDictionary. Defaults to 200.

Root

Root entry for the ModelStateDictionary.

ValidationState
Values

Gets the value sequence.

Methods

Name Description
AddModelError(String, Exception, ModelMetadata)

Adds the specified exception to the Errors instance that is associated with the specified key. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

AddModelError(String, String)

Adds the specified errorMessage to the Errors instance that is associated with the specified key. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

Clear()

Removes all keys and values from this instance of ModelStateDictionary.

ClearValidationState(String)

Clears ModelStateDictionary entries that match the key that is passed as parameter.

ContainsKey(String)

Determines whether the read-only dictionary contains an element that has the specified key.

FindKeysWithPrefix(String)

Gets a ModelStateDictionary.PrefixEnumerable that iterates over this instance of ModelStateDictionary using the specified prefix.

GetEnumerator()

Returns an enumerator that iterates through this instance of ModelStateDictionary.

GetFieldValidationState(String)

Returns the aggregate ModelValidationState for items starting with the specified key.

GetValidationState(String)

Returns ModelValidationState for the key.

MarkFieldSkipped(String)

Marks the ValidationState for the entry with the specified key as Skipped.

MarkFieldValid(String)

Marks the ValidationState for the entry with the specified key as Valid.

Merge(ModelStateDictionary)

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

Remove(String)

Removes the ModelStateEntry with the specified key.

SetModelValue(String, Object, String)

Sets the of RawValue and AttemptedValue for the ModelStateEntry with the specified key.

SetModelValue(String, ValueProviderResult)

Sets the value for the ModelStateEntry with the specified key.

StartsWithPrefix(String, String)

This API supports the MVC's infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

TryAddModelError(String, Exception, ModelMetadata)

Attempts to add the specified exception to the Errors instance that is associated with the specified key. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

TryAddModelError(String, String)

Attempts to add the specified errorMessage to the Errors instance that is associated with the specified key. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

TryAddModelException(String, Exception)

Adds the specified exception to the Errors instance that is associated with the specified key. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

TryGetValue(String, ModelStateEntry)

Explicit Interface Implementations

Name Description
IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

IEnumerable<KeyValuePair<String,ModelStateEntry>>.GetEnumerator()

Returns an enumerator that iterates through the collection.

IReadOnlyDictionary<String,ModelStateEntry>.Keys

Gets an enumerable collection that contains the keys in the read-only dictionary.

IReadOnlyDictionary<String,ModelStateEntry>.Values

Gets an enumerable collection that contains the values in the read-only dictionary.

Extension Methods

Name Description
AddModelError<TModel>(ModelStateDictionary, Expression<Func<TModel,Object>>, Exception, ModelMetadata)

Adds the specified exception to the Errors instance that is associated with the specified expression. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

AddModelError<TModel>(ModelStateDictionary, Expression<Func<TModel,Object>>, String)

Adds the specified errorMessage to the Errors instance that is associated with the specified expression. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

Remove<TModel>(ModelStateDictionary, Expression<Func<TModel,Object>>)

Removes the specified expression from the ModelStateDictionary.

RemoveAll<TModel>(ModelStateDictionary, Expression<Func<TModel,Object>>)

Removes all the entries for the specified expression from the ModelStateDictionary.

TryAddModelException<TModel>(ModelStateDictionary, Expression<Func<TModel,Object>>, Exception)

Adds the specified exception to the Errors instance that is associated with the specified expression. If the maximum number of allowed errors has already been recorded, ensures that a TooManyModelErrorsException exception is recorded instead.

Applies to


Feedback

Was this page helpful?