.NET MVC validating form input field with null value (int?) Not working

Is there any recommended / standard way to implement this kind of MVC code that would validate form fields that are stored as null-valued integers (int?)?

One of the main problems I am facing right now is that if I enter something that is not an integer like "abc123" and try to submit a form, the MVC tech stack will automatically convert that erroneous integer value to empty string before processing any validators. Since empty is a completely acceptable value in my case, the validators will go through, the save form looks like it works right to the end, and everything looks good until the user reopens the updated form and sees only an empty field, not an integer that he or she entered.

I first tried several dataAnnotation-validators to solve this problem, but none of them worked, and the reason is that the erronoeus integer will be converted to an empty string before the value is passed to the validators.

[Integer]
[RegularExpression("[0-9]{1,}")]
public int? MyTroublesomeIntField { get; set; }

      

After that, I tried to fix the problem by creating my own binder by executing IModelBinder. After experimenting with custom model bindings, it seems that the erroneous value is converted to an empty string even before the custom binder is run. Here are a couple of code snippets from my modelBinder:

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]; 

      

So, it seems to me that there is some part of the MVC tech stack that does this conversion (erroneous integer input to empty string value) even before the custom model bindings even start working. I'm just wondering where this is going and is there any way to tweak it to access the original value?

Another alternative that I was already thinking about would be creating a separate row field for each of the int int inputs? form fields that I have on the system. For string fields, even simple DataAnotations like RegularExpressions will work right away and fix the underlying problem. Finally, I just need to add the binding data from all these "dummy" -string values ​​to real int values? Somewhere in the controller / business logic. But it just seems like a massive job since I have a lot of loads of these systems on the system. Feels weird that there would be a lot more work to be done to get a job on the Internet when, at the same time, an exactly like apporach is doubling down? fields that don't currently work with int? s - if I enter erroneous double letters containing fe,client side validation will catch the error.

Any suggestions what would be the best way here?

My tech stack looks something like this: Razor, MVC5, Entity framework,

+3


source to share





All Articles