In my application, I'm using the dataAnotation
as below for validating my ViewModels:
[DisplayName("Provider Business Name") ]
[StringLength(35)]
public string ProviderBusinessName
{
get { return _providerBusinessName; }
set { _providerBusinessName = value.ToUpper(); }
}
My environment is Asp.Net Webforms 4.0 and I'm manually invoking the validation with the following code:
dynamic context = new ValidationContext(datamodel, serviceProvider: null, items: null);
results = new List<ValidationResult>();
return Validator.TryValidateObject(datamodel, context,
results, validateAllProperties: true);
My problem is that the error messages are using the property name instead of the contents of the DisplayName attribute. As far as I can see, the validation attributes should use the DisplayName in the error message.
Try using Display property instead (that will make it work for your validation give you the power to use Resources there to be localized):
[Display(Name = "Provider Business Name")]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With