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 6718161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:56:13+00:00 2026-05-26T08:56:13+00:00

How can I replace the Range values with Web.Config values in MVC3? [Range(5, 20,

  • 0

How can I replace the Range values with Web.Config values in MVC3?

[Range(5, 20, ErrorMessage = "Initial Deposit should be between $5.00 and $20.00")
public decimal InitialDeposit { get; set; }

web.config:

<add key="MinBalance" value="5.00"/>
<add key="MaxDeposit" value="20.00"/>
  • 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-26T08:56:13+00:00Added an answer on May 26, 2026 at 8:56 am

    You will need to create a custom attribute inheriting from RangeAttribute and implementing IClientValidatable.

        public class ConfigRangeAttribute : RangeAttribute, IClientValidatable
        {
            public ConfigRangeAttribute(int Int) :
                base
                (Convert.ToInt32(WebConfigurationManager.AppSettings["IntMin"]),
                 Convert.ToInt32(WebConfigurationManager.AppSettings["IntMax"])) { }
    
            public ConfigRangeAttribute(double Double) :
                base
                (Convert.ToDouble(WebConfigurationManager.AppSettings["DoubleMin"]),
                 Convert.ToDouble(WebConfigurationManager.AppSettings["DoubleMax"])) 
            {
                _double = true;
            }
    
            private bool _double = false;
    
            public override string FormatErrorMessage(string name)
            {
                return String.Format(ErrorMessageString, name, this.Minimum, this.Maximum);
            }
    
            public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                var rule = new ModelClientValidationRule
                {
                    ErrorMessage = FormatErrorMessage(this.ErrorMessage),
                    ValidationType = "range",
                };
                rule.ValidationParameters.Add("min", this.Minimum);
                rule.ValidationParameters.Add("max", this.Maximum);
                yield return rule;
            }
    
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (value == null)
                    return null;
    
                if (String.IsNullOrEmpty(value.ToString()))
                    return null;
    
                if (_double)
                {
                    var val = Convert.ToDouble(value);
                    if (val >= Convert.ToDouble(this.Minimum) && val <= Convert.ToDouble(this.Maximum))
                        return null;
                }
                else
                {
                    var val = Convert.ToInt32(value);
                    if (val >= Convert.ToInt32(this.Minimum) && val <= Convert.ToInt32(this.Maximum))
                        return null;
                }
    
                return new ValidationResult(
                    FormatErrorMessage(this.ErrorMessage)
                );
            }
        }
    

    Example usage:

    [ConfigRange(1)]
    public int MyInt { get; set; }
    
    [ConfigRange(1.1, ErrorMessage = "This one has gotta be between {1} and {2}!")]
    public double MyDouble { get; set; }
    

    The first example will return the default error message, and the second will return your custom error message. Both will use the range values defined in web.config.

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

Sidebar

Related Questions

so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
I need to know how I can replace the last s from a string
Is there a built-in .NET class that can replace or work like WinHttp.WinHttpRequest? Thanks!
I'd like a regexp or other string which can replace everything except alphanumeric chars
Is it possible to develop a plug-in for Internet Explorer that can replace the
Can I replace the maxlength attribute with something in CSS? <input type='text' id=phone_extension maxlength=4
How can I replace lone instances of \n with \r\n (LF alone with CRLF)
How can I replace multiple spaces in a string with only one space in
How can I replace Line Breaks within a string in C#?
How can I replace \r\n in an std::string ?

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.