CultureInfo issue with Modelbinding double in asp.net-mvc(2)
I'm not sure how far localisation goes with the default model binder (DefaultModelBinder), but you can easily create a binder yourself that can handle the culture specific parsing of the data, e.g, create a new class, let's call it the DoubleModelBinder, copypasta the following:
public class DoubleModelBinder : IModelBinder
{
/// <summary>
/// Binds the value to the model.
/// </summary>
/// <param name="controllerContext">The current controller context.</param>
/// <param name="bindingContext">The binding context.</param>
/// <returns>The new model.</returns>
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var culture = GetUserCulture(controllerContext);
string value = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName)
.ConvertTo(typeof(string)) as string;
double result = 0;
double.TryParse(value, NumberStyles.Any, culture, out result);
return result;
}
/// <summary>
/// Gets the culture used for formatting, based on the user's input language.
/// </summary>
/// <param name="context">The controller context.</param>
/// <returns>An instance of <see cref="CultureInfo" />.</returns>
public CultureInfo GetUserCulture(ControllerContext context)
{
var request = context.HttpContext.Request;
if (request.UserLanguages == null || request.UserLanguages.Length == 0)
return CultureInfo.CurrentUICulture;
return new CultureInfo(request.UserLanguages[0]);
}
}
Now, what we are doing here, is establishing our own language-aware double-parser. When we implement the IModelBinder interface, we need to create a BindModel method. This is where the meat of the it is done, but before we can parse anything, we need to get an IFormatProvider based on the browser's language. So, we use the GetUserCulture method to try and ready the browser's language. If we can't revert to the current culture.
When we have that, we are then in a position to parse the value. We first grab it from the ValueProvider (which is really a composite of many value providers, e.g. from the Form collection, Request collection, etc.), and then we parse it using the discovered IFormatProvider, which is the CultureInfo we now have.
Once you've done that, it's pretty trivial to add it to the model binder collection;
ModelBinder.Binders[typeof(Double)] = new DoubleModelBinder();
Try that and see if that helps.