I am having the same problem many people already had - Model Binder doesn't accept localized decimal input. In all of the threads, here and other forums, the recommended solution is implementing a custom ModelBinder
.
My problem is that those solutions somehow don't work for me. Let's use this solution for example: comma decimal seperator in asp.net mvc 5
When I reference all namespaces, two errors remain:
Error CS0115 'DecimalModelBinder.BindModel(ControllerContext, ModelBindingContext)': no suitable method found to override ...
and
Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and 'decimal'
Where the second one references the entire return
statement.
Did something change in the MVC Framework, so this code is outdated, or am I doing something wrong?
The code I ended up with is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.Mvc;
namespace AetMuzickaOprema.App_Start
{
public class DecimalModelBinder : System.Web.ModelBinding.DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, System.Web.ModelBinding.ModelBindingContext bindingContext) //first error
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue); //second error
}
}
}
Model property in question:
[Required]
[Range(typeof(decimal), "0", "999999999")]
public decimal Price { get; set; }