Aspnet Core Decimal binding not working on non English Culture
Apparently, the decimal binding in ASP.NET core 1.0.0 is not culture invariant by default. The model binding depends on the server culture.
You can change this behavior with a custom model binding as suggested by Stephen Muecke. Here is mine based on Custom Model Binding in ASP.Net Core 1.0 (RTM)
public class InvariantDecimalModelBinderProvider : IModelBinderProvider{ public IModelBinder GetBinder(ModelBinderProviderContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); if (!context.Metadata.IsComplexType && (context.Metadata.ModelType == typeof(decimal) || context.Metadata.ModelType == typeof(decimal?))) { return new InvariantDecimalModelBinder(context.Metadata.ModelType); } return null; }}public class InvariantDecimalModelBinder : IModelBinder{ private readonly SimpleTypeModelBinder _baseBinder; public InvariantDecimalModelBinder(Type modelType) { _baseBinder = new SimpleTypeModelBinder(modelType); } public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) throw new ArgumentNullException(nameof(bindingContext)); var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (valueProviderResult != ValueProviderResult.None) { bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult); var valueAsString = valueProviderResult.FirstValue; decimal result; // Use invariant culture if (decimal.TryParse(valueAsString, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result)) { bindingContext.Result = ModelBindingResult.Success(result); return Task.CompletedTask; } } // If we haven't handled it, then we'll let the base SimpleTypeModelBinder handle it return _baseBinder.BindModelAsync(bindingContext); }}
And in Startup.cs:
services.AddMvc(config =>{ config.ModelBinderProviders.Insert(0, new InvariantDecimalModelBinderProvider());});