I’m using datamodel validation on my asp.net MVC website and I encountered an error.
My model looks like this:
public class MyModel{
public String Name{get;set;}
public double? Value {get;set;}
}
It works fine in most case, but if I’ve a decimal value in the “Value” field, like “5.5”, I got a validation error.
The website is running under the “Fr-FR” locale. I tried “5.5” or “5,5”, they both don’t work but I got different messages:
La valeur '5.5' n'est pas valide pour Value.
which should mean "The value '5.5' isn't valide for Value"
And
Le champ Value doit être un nombre.
Which means "The field Value has to be a number"
The only “logic that I’ve in my code behind is that I use “ModelState.IsValid” before doing an update.
Why is this not working and what should I do to correct this problem?
Important edit
By debugging I saw that fields of my models comes “NULL” when having numbers with decimals.
In fact the problem is that the client validation seems to works with EN culture and the server with the fr-FR validation.
So I wrote a simple modelBinder which is culture invariant:
And I register it in the Global.asax file:
And now it works 🙂