This is how mvc.net core 3.1 - This is is how my property is in the class
[BindProperty]
[Required(ErrorMessage = "Enter the valid amount")]
[ValidDecimal(ErrorMessage = "Enter the amount correctly")]
public decimal? QuoteAmountTotal { get; set; }
And code for custom ValidDecimal value is
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class ValidDecimalAttribute : ValidationAttribute{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value == null || value.ToString().Length == 0)
{
return ValidationResult.Success;
}
return decimal.TryParse(value.ToString(), out _) ? ValidationResult.Success : new ValidationResult(ErrorMessage);
}
}
I am inputing value in this field with space or alpha numeric. For example 2 0 0 0. However, it displays default mvc.net core error instead of my custom error which is
The value '2 0 0 0' is not valid for QuoteAmountTotal.
This is AttemptedvalueisInvalidAccessor enter image description here
I need to display my custom error message instead of default MVC error model message, which is not displaying in this case.
