Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Asp.net core 2.0 comma and point decimal separator

$
0
0

Hello,

I tried to make an attempt of a basic project Core 2 with just a model:

 public class Bilance 
    { 
        [Key] 
        public int Id { get; set; } 

        [Required] 
        public decimal Amount{ get; set; } // (sql 18,2) 
  }

I used this Custom Model Binding to use the comma as decimal seperator but it doesn't work.

<div id="m_-8531784082358642913divtagdefaultwrapper" dir="ltr">

I used this Custom Model Binding to use the comma as decimal seperator but it doesn't work:

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;

                // Use invariant culture
                if (decimal.TryParse(valueAsString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal 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);
        }
    }

</div> <div dir="ltr">********************</div> <div dir="ltr"></div> <div dir="ltr">        public void ConfigureServices(IServiceCollection services)
        {
            // ....
            services.AddMvc(config =>
            {
                config.ModelBinderProviders.Insert(0, new InvariantDecimalModelBinderProvider());
            });</div>

Works with a few numbers (123,456) with others no (11,50... 1520,77)

Could you help me? 

Thanks


Viewing all articles
Browse latest Browse all 9386

Trending Articles