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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:48:45+00:00 2026-06-12T20:48:45+00:00

Something I’ve found quite frustrating in ASP.Net MVC is that the default model binder

  • 0

Something I’ve found quite frustrating in ASP.Net MVC is that the default model binder implicitly applies the Required annotation when binding an empty (string or null) value to a non-nullable value type instead of simply leaving the target with its default value, or at least providing an option to allow that to be the default behaviour.

Given a scenario where it’s inconvenient to change the target property type on the model to a nullable value, what’s the shortest amount of code I can employ to allow the default model binder to simply skip its attempt to bind an empty value to a non-nullable value type? I’m assuming I’ll need to subclass DefaultModelBinder, but I’m not sure what I need to override to achieve the desired behaviour.

example:

<input type="text" name="MyField"/>

Submit without a value:

public ActionResult MyAction(MyModel model)
{
    // do stuff
}

public class MyModel
{
    public int MyField { get; set; }
}

The property MyField should be allowed to retain its default value of 0 seeing as an empty value was posted from the form.

Assume that I can’t simply change the property type a Nullable<int>.

  • 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-12T20:48:47+00:00Added an answer on June 12, 2026 at 8:48 pm

    How about something like this? (Disclaimer: Not tested to any degree of confidence)

    public class NonRequiredModelBinder : DefaultModelBinder
    {
        protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
        {
            var result = base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
            if (result == null && propertyDescriptor.PropertyType.IsValueType)
                return Activator.CreateInstance(propertyDescriptor.PropertyType);
    
            return result;
        }
    }
    

    The idea — in theory — is to determine what value the DefaultModelBinder assigned to the property, check if it was a null value and then assign it to the default value of the ValueType that is being bound.

    This should prevent the binder from adding ModelState errors, and still would not affect the validation of other attributes such as [Range]

    I would recommend taking this a step further and creating your own attribute (i.e., NonRequiredAttribute). Then in your custom model binder you could check to see if the property has the new NonRequired attribute, and execute this custom code only in the case that it does.

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

Sidebar

Related Questions

Something odd going on here. I have some JS that posts to a ASP.NET
Something like this question: Distinguish between null and missing parameters in ASP.NET MVC4 ...except
Something that seems to be absent from the otherwise great new features for Windows
Something that I think is really strange is happening when I use die() .
Something I've wanted to learn for quite a time now, but haven't been able
Something strange happens on a text parser that I am writing an I thought
Something like my_model.data_dict['my_field'] Which would give you the current value for that field? Need
Something happens in my system on a worker thread. Let's say that the state
Something I encountered today at work, I tried to make a view that used
Something that I would assume comes up a lot... I'd like to know if

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.