This is a problem that has been talked about for a long time and at least those of us who have been working with asp mvc since 2010 have had it.
And now in ASP Core 3.x it has not been the difference, now that I am developing a web application from scratch I ran into this old enemy.
And what's the problem? Well, it doesn't matter if you're using American regional comma(,) for thousands and dot(.) for decimals or European dot(.) for thousands and comma(,) for decimals, jquery always triggers the error when validating the field. .
The error is not from asp core, it is actually from jquery that always expects the number to be 2500.25 if with an American decimal point, which makes it a problem for those of us who work with applications that are used in both America and Spain.
I corrected it by making the decimals in my model a string and then converting it to a decimal in the entity, but although this type of conversion and validation is negligible, it takes time and slows down your application.
But I wanted to take advantage of a feature of ASP Core where you can manipulate the model and I think it's a much more natural solution in the style of an MVP(Most Valuable Professional) :D, although I'm not but I try.
In my model I have something like this:
First I remove the validation for all the decimal fields for the view that way jquery doesn't send me the error in question and I do it like this for input:
<script type="text/javascript"> $("#neto").removeAttr("data-val"); </script>
With jquery I manipulate the input so that I put a thousands and decimals separator, I do not publish it because there are many methods on the web and I am sure they are better than mine.
I am going to bind the decimal type and for that I will create a couple of classes inside a Binder folder, which can have any name.
Code:
If you look, we work with the IModelBinderProvider interface and we indicate that when the model reaches a Decimal type, it implements the DecimalModelBinder() class.
That I also leave them here with comments:
Only one last thing is needed is to make the injection in the Startup.cs in the "ConfigureServices(IServiceCollection services)" section we add the following:
And well, it's like this, we can manipulate the model in an elegant way in ASP Core 3.x. Any questions, let me know.