Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 十进制模型绑定器不适用于全球化_C#_Jquery_Asp.net Mvc - Fatal编程技术网

C# 十进制模型绑定器不适用于全球化

C# 十进制模型绑定器不适用于全球化,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我正在将一些数据从客户端发送到服务器,如下所示 $.post(url, myData) .done(function (data) { }); 这是控制器的动作 public class MyModel { decimal Precision { get; set; } } [HttpPost] public ActionResult PostInfo(MyModel postBack) { } 当我使用英语文化时,PostInfo会像预期的那样工作,但是当我将文化更改为

我正在将一些数据从客户端发送到服务器,如下所示

$.post(url, myData)
   .done(function (data) {
   });
这是控制器的动作

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

[HttpPost]
public ActionResult PostInfo(MyModel postBack)
{
}
当我使用英语文化时,PostInfo会像预期的那样工作,但是当我将文化更改为西班牙语且精度=1,2时,我会得到以下错误

值1,2对于精度无效

有人能告诉我为什么当CurrentCulture是西班牙语时默认的模型绑定器无法解析1,2吗

我更改了_Layout.cshtml中的区域性。这只是为了测试

@{
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("es", "ES");
}

上次我检查时,默认的模型绑定器不处理国际化格式。你必须自己滚

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;
    }
}

“当我将文化改变为西班牙语时”-你能分享你在哪里以及如何做到这一点吗?@pep我编辑了这个问题来展示我如何改变文化啊,这行不通。视图渲染在调用控制器后发生。有关设置当前区域性的几种方法,请参见。我建议这样做。根据这篇博客,文章的内容正好相反,只提到了日期。他说,英国的日期格式在本地不受支持,甚至给出了一个如何编写模型绑定器来修复这种情况的示例。