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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:35:30+00:00 2026-06-05T00:35:30+00:00

My view model has a [Required] non-nullable int property, selected by a DropDownListFor. If

  • 0

My view model has a [Required] non-nullable int property, selected by a DropDownListFor. If the list to choose from is empty, ModelState.IsValid is true.

My model has a Required int property:

public class MyModel
{
    [Required]
    public int PickedValue  { get; set;}

    IEnumerable<SelectListItem> Items { get; set; }
}

In my Create view, I render a <select> element:

@Html.DropDownListFor(model => model.PickedValue, model.Items)

And in my controller, if the model.Items list is empty (no elements to choose from), ModelState.IsValid is true:

[HttpPost]
public ActionResult Create( MyModel model )
{
    if( ModelState.IsValid )
    {
        // true, and ModelState.Keys doesn't contain PickedValue because it was never POSTed.
    }
    //...
 }

Now, the problem goes away if:

  1. PickedValue is Nullable (int?), or
  2. if I have an empty item in my <select> – @Html.DropDownListFor(model => model.PickedValue, model.Items, ""), or
  3. if client-side validation is enabled (since the action is never fired).

Is there a way to force ModelState.IsValid to be false if some of MyModel‘s properties are [Required] but are missing from the POST data? Shouldn’t this be the default behavior?

  • 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-06-05T00:35:32+00:00Added an answer on June 5, 2026 at 12:35 am

    In your case, I think it would probably be better to make PickedValue a nullable int. If there is a chance that your DropDownList has no items, then null should be a possible value for your PickedValue.

    Reply to comments

    That makes sense but doesn’t answer my original question… Is there a
    way to force ModelState.IsValid to be false if some of MyModel’s
    properties are [Required] but are missing from the POST data?
    Shouldn’t this be the default behavior?

    When it comes to ints, doubles, bools, and other primitives that cannot have a null value, when the DefaultModelBinder receives no data for them in the HTTP request, it must still construct a model instance. Therefore, the model gets constructed and the properties are initialized to their default values. In the case if int, this means it will be initialized to zero. Since nothing is received in the HTTP request, this means the int never gets set, hence it will always be zero. Have you tried giving your int a [Range] validator instead of a [Required] validator? This should validate that it is not zero:

    [Range(1, int.MaxValue)]
    public int PickedValue  { get; set; }
    

    I’d like to avoid changing the model because it’s already a common
    pattern in my code… Plus making a property in the model nullable to
    force it not to be null seems counter-intuitive.

    Just because it’s a common pattern in your code doesn’t mean it’s correct. I have to reiterate my original answer: If it is possible for your dropdown list to not have any items, then your model should allow null for its selected value.

    This is one reason why it’s good practice to have separate entity & viewmodel layers. In your domain entities, a relationship might be required. However when a user is first presented with a form to select that relationship, you either have to give them a default or empty selected dropdown item. In this case, the foreign key in the entity might be an int, but the representation in the viewmodel should be a Nullable<int>. For this reason alone I don’t think it’s counter-intuitive to make something nullable just to make sure it is not null. You make it nullable in the viewmodel so you can give the user a form with a blank value, and require them to fill it in.

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

Sidebar

Related Questions

In my view model if have a: List<Car> where car has an Id and
My view page has a Model which is represented by a List of Students.
My view model defines property which has to be displayed as combo box. Property
I added ValidationSummary Html helper for my View Model class which has 5 required
I have Listbox which is bound to my view model. This view model has
i have a strongly typed asp.net-mvc view and the Model has a . Links
I've a tool bar which has its own view model. I've used command binding
I have a Backbone View that has a Collection as its Model. If the
I have a business model called Customer which has many required properties (via DataAnnotations)
I have a basic MVC view model with annotations, for example: [Required(ErrorMessage=Your Name Required)]

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.