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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:02:39+00:00 2026-05-15T01:02:39+00:00

I have a [Range] annotation that looks like this: [Range(0, 100)] public int AvailabilityGoal

  • 0

I have a [Range] annotation that looks like this:

[Range(0, 100)]
public int AvailabilityGoal { get; set; }

My webpage looks like this:

<%=Html.TextBoxFor(u => u.Group.AvailabilityGoal)%>

It works as it should, I can only enter values between 0 and 100 but I also want the input box to be optional, the user shouldn’t get an validation error if the input box is empty. This has nothing to do with the range but because the type is an integer. If the user leaves it empty it should make AvailabilityGoal = 0 but I don’t want to force the user to enter a zero.

I tried this but it (obviously) didn’t work:

[Range(typeof(int?), null, "100")]

Is it possible to solve this with Data Annotations or in some other way?

Thanks in advance.

Bobby

  • 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-15T01:02:40+00:00Added an answer on May 15, 2026 at 1:02 am

    I guess you could override the Range object and add this behaviour.

    public class OptionalRange : RangeAttribute {
        public override bool IsValid(object value) {
            if (value == null || (int)value == 0) return true;
            return base.IsValid(value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a range of items in an HTML drop-down menu that I need
I have range of numbers that range from 1 - 0.00000X . Most are
I have two range sliders in my page, and I would like to implement
We have a range of Classic ASP applications that we are looking to port
I have a range of tables that I have normalized for a user profile
I have a range of data that I have approximated using a polynomial of
I have a Range variable that contains an address - $2:$2,$4:$205,$214:$214 - (3 groups
I currently have: Range(Z1).Interior.Color = RGB(255, 255, 255) But this wipes out the borders
I have this range field and near it is the default value of 300
Say I have a range of data from A1:Y55 containg data like 123 UID

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.