ModelStateDictionary.TryAddModelError Method

Definition

Overloads

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.

TryAddModelError(String, Exception, ModelMetadata)

Source:
ModelStateDictionary.cs
Source:
ModelStateDictionary.cs
Source:
ModelStateDictionary.cs

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.

C#
public bool TryAddModelError (string key, Exception exception, Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata metadata);

Parameters

key
String

The key of the ModelStateEntry to add errors to.

exception
Exception

The Exception to add. Some exception types will be replaced with a descriptive error message.

metadata
ModelMetadata

The ModelMetadata associated with the model.

Returns

True if the given error was added, false if the error was ignored. See MaxAllowedErrors.

Applies to

ASP.NET Core 9.0 and other versions
Product Versions
ASP.NET Core 1.0, 1.1, 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0, 7.0, 8.0, 9.0

TryAddModelError(String, String)

Source:
ModelStateDictionary.cs
Source:
ModelStateDictionary.cs
Source:
ModelStateDictionary.cs

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.

C#
public bool TryAddModelError (string key, string errorMessage);

Parameters

key
String

The key of the ModelStateEntry to add errors to.

errorMessage
String

The error message to add.

Returns

True if the given error was added, false if the error was ignored. See MaxAllowedErrors.

Applies to

ASP.NET Core 9.0 and other versions
Product Versions
ASP.NET Core 1.0, 1.1, 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0, 7.0, 8.0, 9.0