Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 728277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:39:47+00:00 2026-05-14T06:39:47+00:00

I need to set the ErrorMessage property of the DataAnnotation’s validation attribute in MVC

  • 0

I need to set the ErrorMessage property of the DataAnnotation’s validation attribute in MVC 2.0. For example I should be able to pass an ID instead of the actual error message for the Model property, for example…

[StringLength(2, ErrorMessage = "EmailContentID")] 
[DataType(DataType.EmailAddress)]         
public string Email { get; set; } 

Then use this ID (“EmailContentID”) to retrieve some content(error message) from a another service e.g database. Then the error error message is displayed to the user instead of the ID. In order to do this I need to set the DataAnnotation validation attribute’s ErrorMessage property.

It seems like a stright forward task by just overriding the DataAnnotationsModelValidatorProvider‘s protected override IEnumerable GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes)

However it is complicated now….

A. MVC DatannotationsModelValidator’s ErrorMessage property is readonly. So I cannot set anything here
B. System.ComponentModel.DataAnnotationErrorMessage property(get and set) which is already set in MVC DatannotationsModelValidator so I cannot set it again. If I try to set it I get “The property cannot set more than once…” error message.

 public class CustomDataAnnotationProvider : DataAnnotationsModelValidatorProvider 
 { 
      protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata,  ControllerContext context, IEnumerable<Attribute> attributes) 
       { 
            IEnumerable<ModelValidator> validators = base.GetValidators(metadata, context, attributes); 

             foreach (ValidationAttribute validator in validators.OfType<ValidationAttribute>()) 
             { 
                 messageId = validator.ErrorMessage; 
                 validator.ErrorMessage = "Error string from DB And" + messageId ; 
             } 

            //...... 
       } 
  } 

Can anyone please give me the right direction on this?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:39:48+00:00Added an answer on May 14, 2026 at 6:39 am

    I personally wouldnt create a new provider inheriting the original provider but I would create a new Attribute inheriting either the ValidationAttribute or the StringLengthAttribute.

    Something Like this

    public class MyNewValidationAttribute : StringLengthAttribute
    {
        public int ErrorMessageId
        {
            set { 
                    var myErrorMessageFromDB = ""; //query database and get message.
                    this.ErrorMessage = myErrorMessageFromDB;  
                }
        }
    }
    

    Now when you assign the attribute

    [StringLength(2, ErrorMessageId = 1 //equals id from database
    )] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.