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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:31:54+00:00 2026-05-25T01:31:54+00:00

How do I validate a select list for a value of 0? I have

  • 0

How do I validate a select list for a value of 0?

I have tried the following

RuleFor(x => x.ProductId).Equal(0).WithMessage(required);
RuleFor(x => x.ProductId).NotEqual(0).WithMessage(required);
RuleFor(x => x.ProductId).GreaterThan(0).WithMessage(required);
RuleFor(x => x.ProductId).GreaterThan(0).When(x => x.ProductId < 1).WithMessage(required);
//etc.

My select list has a value of ‘0’ for ‘Select…’. If I choose that value in my form, either the ModelState.IsValid throws an error (‘not expected range’) or if I comment out the ModelState.IsValid then a value of 0 gets posted to the db. Either way no validation is occurring. All my other fields which are a mix of strings, int, and boolean work fine and validate correctly.

Note: ProductId is a int in my model and view model.

Lost..

  • 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-25T01:31:54+00:00Added an answer on May 25, 2026 at 1:31 am

    I solved the problem. This is a question I wished I had never asked. The solution was a comedy of errors but I will nonetheless share what I discovered along the way.

    The short answer is:

    RuleFor(x => x.ProductId).GreaterThan(0).WithMessage(required);
    

    The problem was in my controller and (embarrassing enough) my view.

    1. I forgot to add my validationmessagefor ProductId. But this did not solve the problem entirely.
    2. My controller looked like this.
    
    [HttpPost]
    public ActionResult Update(ProductModel model)
    {
      if(!ModelState.IsValid)
          throw new ArgumentExpection();
    //etc.
    }
    

    I originally was testing my model to work with Automapper and once I implemented validation I forgot to remove the ArgumentException.

    What this did was never give my validation rule the light of day. Of course it never validated because the controller was told to throw an error ABOUT the ModelState instead of post back the ACTUAL validation error. Dohhh!

    [HttpPost]
    public ActionResult Update(ProductModel model)
    {
      if(!ModelState.IsValid)
         return View(model);//pssst! the validation error is in here.
    //etc.
    }
    

    I do have a better understanding of the order of things now. Hope this helps someone else who is dumb as I was.

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

Sidebar

Related Questions

I need to validate this simple pick list: <select name=<%= key %>> <option value=ETC
I have a select list that's default value (for Please Select) is 0. this
I'm using the jQuery plugin Validation to validate a form. I have a select
I have an array of options for a select list. $options = array( 1=>'Option1',
I have the following code to validate some fields on a sharepoint newform.aspx. To
How can I validate that my ASPNET AJAX installation is correct. I have Visual
I want to validate the value a user enters in a text box, so
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
Let's say I have the following selectlist (Countries) in a ViewModel: //.. private static
I'm trying to get the selected value from a drop down list via jQuery.

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.