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

  • Home
  • SEARCH
  • 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 3443006
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:44:19+00:00 2026-05-18T08:44:19+00:00

There’s a way to set the default resource to the data annotations validations? I

  • 0

There’s a way to set the default resource to the data annotations validations?

I don’t wanna make something like this:

[Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)]
public string Name { get; set; }

I would like something like this:

Global.asax

DataAnnotations.DefaultResources = typeof(CustomDataAnnotationsResources);

then

[Required]
public string Name { get; set; }

someone gimme a light!

thanks in advance

EDIT

My real problem was with EF Code First CTP4. CTP5 fix it. Thanks for everyone.

  • 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-18T08:44:20+00:00Added an answer on May 18, 2026 at 8:44 am

    You could try doing this:

    Add this class somewhere in your project:

     public class ExternalResourceDataAnnotationsValidator : DataAnnotationsModelValidator<ValidationAttribute>
    {
        /// <summary>
        /// The type of the resource which holds the error messqages
        /// </summary>
        public static Type ResourceType { get; set; }
    
        /// <summary>
        /// Function to get the ErrorMessageResourceName from the Attribute
        /// </summary>
        public static Func<ValidationAttribute, string> ResourceNameFunc 
        {
            get { return _resourceNameFunc; }
            set { _resourceNameFunc = value; }
        }
        private static Func<ValidationAttribute, string> _resourceNameFunc = attr => attr.GetType().Name;
    
        public ExternalResourceDataAnnotationsValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
            : base(metadata, context, attribute)
        {
            if (Attribute.ErrorMessageResourceType == null)
            {
                this.Attribute.ErrorMessageResourceType = ResourceType;
            }
    
            if (Attribute.ErrorMessageResourceName == null)
            {
                this.Attribute.ErrorMessageResourceName = ResourceNameFunc(this.Attribute);
            }
        }
    }
    

    and in your global.asax, add the following:

    // Add once
    ExternalResourceDataAnnotationsValidator.ResourceType = typeof(CustomDataAnnotationsResources);
    
    // Add one line for every attribute you want their ErrorMessageResourceType replaced.
    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RangeAttribute), typeof(ExternalResourceDataAnnotationsValidator));
    

    It will look for a property with the same name as the validator type for the error message. You can change that via the ResourceNameFunc property.

    EDIT: AFAIK this works from MVC2 onwards, as DataAnnotationsModelValidatorProvider was introduced in MVC2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There was some code like this: // Convenience to make things more legible in
There are things like f.call(...) f.apply(...) But then there's this (1, alert)('Zomg what is
There is this SQL Statement SELECT t1.Name ,Count(t2.SubID) Totals -- I don't know how
There is previous little on the google on this subject other than people asking
There are a few ways to get class-like behavior in javascript, the most common
For some reason, after submitting a string like this Jack’s Spindle from a text
There are several shading languages available today like GLSL, HLSL, CG, which one to
There is this strange situation I'm fighting on. I have 3 pages, les call
There doesn't seem to be any tried and true set of best practices to
There must be a simple solution to this, but after 4 hours of browsing

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.