Inconsistent culture - decimal separator ignored in model binding between razor view and viewmodel

442 Views Asked by At

I have the following behaviour in my program:

User input for a decimal variable

A) jquery validation turned off:
1) If the user uses a comma as decimal separator, the value is stored correctly in the ViewModel
2) If the user uses a point as decimal separator, the value is multiplied by 100 (as if there was no decimal separator)

B) jquery validation turned on:
1) I get an error, that a number must be supplied
2) same Problem as A2)

However if I display a decimal value of the ViewModel in the view it is shown per default with a point as a decimal separator.
This inconsistency is confusing me and I would like to implement a consistent behaviour, but unfortunately I don't know what I am actually looking for.

The website will be localized in german and italian. The localization works without any problems, so far. This is my

startup.cs

namespace XXX
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Added - uses IOptions<T> for your settings.
            services.AddOptions();

            // Added - Confirms that we have a home for our DemoSettings
            services.Configure<DatabaseSettings>(Configuration.GetSection("DatabaseSettings"));


            services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddAuthentication(IISDefaults.AuthenticationScheme);
            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseStatusCodePagesWithReExecute("/Error/Index", "?i_statusCode={0}");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }



            app.UseStaticFiles();

            IList<CultureInfo> supportedCultures = new List<CultureInfo>
            {
                    new CultureInfo("de"),
                    new CultureInfo("it"),
            };


            var localizationOptions = new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("de"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            };



            var requestProvider = new RouteDataRequestCultureProvider();
            localizationOptions.RequestCultureProviders.Insert(0, requestProvider);

            app.UseRouter(routes =>
            {
                routes.MapMiddlewareRoute("{culture=de}/{*mvcRoute}", subApp =>
                {
                    subApp.UseRequestLocalization(localizationOptions);

                    subApp.UseMvc(mvcRoutes =>
                    {
                        mvcRoutes.MapRoute(
                            name: "defaultLocalized",
                            template: "{culture=de}/{controller=Contract}/{action=Index}/{id?}");

                        mvcRoutes.MapRoute(
                          name: "error",
                          template: "Error/Index",
                          defaults: new { controller = "Error", action = "Index", culture = "de" });

                        mvcRoutes.MapRoute(
                          name: "default",
                          template: "{*catchall}",
                          defaults: new { controller = "Home", action = "Index", culture = "de" });
                    });
                });
            });
        }
    }
}
0

There are 0 best solutions below