0

I'm posting some data from the client to the server like this

$.post(url, myData)
   .done(function (data) {
   });

here's the controller's action

public class MyModel
{
   decimal Precision { get; set; }
}

[HttpPost]
public ActionResult PostInfo(MyModel postBack)
{
}

when I use the English culture PostInfo works as expected however when I change the culture to Spanish and Precision = 1,2 then I get the following error

The value 1,2 is not valid for Precision

Can someone please tell me why the default model binder is failing to parse 1,2 when the CurrentCulture is Spanish?

I change the culture in _Layout.cshtml. It's for testing purpose only.

@{
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("es", "ES");
}
vcRobe
  • 1,671
  • 3
  • 17
  • 35
  • "When I change the culture to Spanish" - can you share where and how you are doing this? – p e p Jul 06 '17 at 16:26
  • @pep I've edited the question to show how I change the culture – vcRobe Jul 06 '17 at 16:31
  • Ah, that won't work. View rendering happens after the controller is invoked. See https://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app for several methods of setting the current culture. I suggest this one https://stackoverflow.com/a/6788690/1558122. – p e p Jul 06 '17 at 16:40

1 Answers1

0

the last time i checked, the default model binder doesn't handle internationalization formats. you would have to roll you own.

public class DecimalModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        var valueResult = bindingContext.ValueProvider
            .GetValue(bindingContext.ModelName);
        var modelState = new ModelState { Value = valueResult };
        object actualValue = null;
        try
        {
            //Check if this is a nullable decimal and a null or empty string has been passed
            var isNullableAndNull = (bindingContext.ModelMetadata.IsNullableValueType &&
                                      string.IsNullOrEmpty(valueResult.AttemptedValue));

            //If not nullable and null then we should try and parse the decimal
            if (!isNullableAndNull)
            {
                actualValue = decimal.Parse(valueResult.AttemptedValue, NumberStyles.Any, CultureInfo.CurrentCulture);
            }
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}
Fran
  • 6,440
  • 1
  • 23
  • 35
  • according to this blog https://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization it does – vcRobe Jul 06 '17 at 17:43
  • That article says the exact opposite and only mentions dates. He says British date formats aren't natively supported and even gives an example of how to write a model binder to fix the situation. – Fran Jul 06 '17 at 17:49