I'm working on a MVC4 application where there's a field with a Data Mask like 000000,00
.
Since the main culture of the project is pt-BR, this is correct and works as expected.
The problem is that some of our developers have their cultures set to english, and when submitting this to a server set as en-US, a validation error occurs, because the method expects 000000.00
instead of 000000,00
.
I thought about making a js fix to check the browser language and change the mask accordingly, but if the browser is set to english and the server to portuguese it'd fail (some of our testers are in this exact scenario).
My plan B is to use DataAnnotation so the server knows the culture it should use when validating the input.
[MagicValidation('pt-BR'))]
public virtual double? myProp { get; set; }
Considering I can't assume the server will be in one set language, my doubts are:
Is my approach correct?
Is there any other easier way of doing it?
If I'm on the right track, is there an existing annotation that already does this?
Thanks in advance for your attention
Have you tried to force the culture in your web.config like this:
I ended up on using this custom model binder, using only a
NumberFormatInfo
as the second parameter of theConvert.ToDouble()
(instead of aCultureInfo
). Here's theNumberFormatInfo
I used:The rest is just a copy/paste of the linked solution