1 year ago

#64326

test-img

Eugene

How to make correctly parse numbers with decimals in MVC actions with multiple cultures supported?

I have MVC POST action requests that can't parse correctly numbers with decimals when there are many cultures supported, for example:

I have a pure form that doesn't use AJAX, it does the usual POST. When the culture is en-US(web front-end), the action receives the doubles/decimals/floats with the decimal separator correctly set, meanwhile if the culture is it-IT those numbers are parsed as if there's no decimal separator, so 4,2(actually "4.2" as in the note) becomes 42.

[Note] In the HttpContext.Request.Form the numbers do arrive as "4.2"

Here's my request localization setup in Startup.cs:

services.AddRequestLocalization(options =>
        {
            options.DefaultRequestCulture = new RequestCulture("en-US");

            options.SupportedCultures =
                options.SupportedUICultures = new[]
                    {
                        new CultureInfo("en-US"),
                        new CultureInfo("it-IT")
                    };
            options.RequestCultureProviders = new List<IRequestCultureProvider>()
            {
                new AcceptLanguageHeaderRequestCultureProvider
                {
                    Options = options
                }
            };
        });

I looked up both on web and here without finding a solution to this, this is the first MVC project that this weird thing when they are moving it to multiple cultures support(before it was just en-US) meanwhile on other projects with same framework they didn't show this behavious. Any clue?

c#

asp.net-core-mvc

globalization

0 Answers

Your Answer

Accepted video resources