title: .NET MVC validation of nullable integer (int?) form input field not working
body: <p>Is there any recommended/standard way to implement such MVC-code that a validation of form fields that are stored as nullable integers (int?) would work? </p>
  
  <p>One of the root problems I'm having with this now, is that if I input something that is not integer like "abc123" and try to submit the form, the MVC tech stack will automatically convert this erroneous integer value to empty string before any validators are processed. Because empty is totally acceptable value in my case, validators will pass, form saving looks like proceeding correctly to the end and everything looks good until the user opens the updated form again and sees only empty field instead of the integer he or she inputted.</p>
  
  <p>At first, I tried multiple dataAnnotation-validators to solve this problem, but none of them worked and the reason seems to be the fact that the erronoeus integer will be converted to empty string before the value is passed to validators.</p>
  
  <pre><code>[Integer]
  [RegularExpression("[0-9]{1,}")]
  public int? MyTroublesomeIntField { get; set; }
  </code></pre>
  
  <p>After this I tried to solve the problem by creating custom model binder by implementing IModelBinder. After experimenting with custom model binders, it seems that the erroneous value gets converted to empty string even before the custom model binder starts to run. Here's couple of code snippets from my modelBinder:</p>
  
  <pre><code>var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  //f.e. this returns empty string 
  value.AttemptedValue 
  //f.e. this also returns empty strings, even though name "raw" leads one to      think that this is the value no one has touched.
  value.RawValue 
  //even this returned empty string even though one could think that by accessing the field from HttpRequest would give access to the truly original value inputted by the user, but no, mvc stack gets to tinker this one as well.
  string valueBefore = HttpContext.Current.Request.Form[bindingContext.ModelName]; 
  </code></pre>
  
  <p>So it seems to me, that there is some part of MVC tech stack that makes this conversion (erroneous integer input to empty string value) even before the custom model binders start to work. I'm just wondering, where is this happening, and is there a chance to customize it to get access to the original value?</p>
  
  <p>Other alternative I have been thinking by now would be to create a separate string field for each of the free input int? form fields I have in the system. For string fields even those simple dataAnnotations like RegularExpressions would work straight away and the main problem would be solved. Finally I would just need to add binding data from all these "dummy"-string values to real int?-values somewhere in the controller/business logic. But this just seems like a massive work as I'm having loads of these ints in the systm. It feels strange that there would be this much extra work to do in order to get int?s working, when at the same time, exactly similar apporach works pefectly with double? fields which is currently not working with int?s - if I input erroneous double containing f.e. letters, client side validation will catch the error.</p>
  
  <p>Any suggestions what would be the best way to proceed here?</p>
  
  <p>My tech stack is pretty much like this: Razor, MVC5, Entity framework, </p>