Model binding in ASP.NET MVC is great, but it follows locale settings. In my locale decimal separator is comma (','), but users use dot ('.') too, because they are lazy to switch layouts. I want this implemented in one place for all decimal
fields in my models.
Should I implement my own Value Provider (or event Model Binder) for decimal
type or I've missed some simple way to do this?
Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.
Unfortunately, you cannot change the decimal separator just for PowerPoint. PowerPoint uses the separators defined in the operating system for the language region. Changing them requires changes that are permanent and system-wide.
go to Start > Control Panel > Regional and Language Options | Windows 10 (Start >type Control Panel and press enter > Region) Click Additional Settings. For Decimal Symbol, enter a dot: .
Cleanest way is to implement your own model binder
public class DecimalModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue); // of course replace with your custom conversion logic } }
And register it inside Application_Start():
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());
Credits : Default ASP.NET MVC 3 model binder doesn't bind decimal properties
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With