Answer the question
In order to leave comments, you need to log in
ASP.NET MVC why does model validation ( Validator.TryValidateObject ) contain default ErrorMessages and not our own?
asp.net mvc 5 application.
global.asax defines custom adapter for RequiredAttribute
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(CustomRequiredAttributeAdapter));
public CustomRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
: base(metadata, context, attribute)
{
if (string.IsNullOrEmpty(attribute.ErrorMessage))
{
attribute.ErrorMessageResourceType = typeof(ValidationResources);
attribute.ErrorMessageResourceName = "RequiredAttribute_ValidationError";
}
}
var isValid = Validator.TryValidateObject(instance, validationContext, validationResults, true);
... Answer the question
In order to leave comments, you need to log in
Only one of them should be set - either ErrorMessageString or ErrorMessageResourceName - but not both. Try explicitly setting ErrorMessage to null. Something like this:
[Required(ErrorMessage = null, ErrorMessageResourceName = “RequiredAttribute_ValidationError”, ErrorMessageResourceType = typeof(ValidationResources))]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question