4

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.

2 Answers 2

5

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")]
Sign up to request clarification or add additional context in comments.

1 Comment

I'm working on a .net 7 application and this solution doesn't work. For e.g., I'm getting ConfirmPassword display name as "Confirm Password" from a resource file, but the response in the web api is still showing ConfirmPassword
1

Have you tried:

[Display(Name = "Provider Business Name")]

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.