MVC3全球化问题

我正在开发一个带有下一个文化设置的MVC3应用程序:

 

首先,当我传递一个整数时,我将视图中的值传递给控制器​​,但是当我传递一个double(3.2)时,控制器中的值返回0,如本问题所述 。 好的模型粘合剂添加:

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); ModelBinders.Binders.Add(typeof(double), new DoubleModelBinder()); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } public class DoubleModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult valueResult = bindingContext.ValueProvider .GetValue(bindingContext.ModelName); ModelState modelState = new ModelState { Value = valueResult }; object actualValue = null; try { actualValue = Convert.ToDouble(valueResult.AttemptedValue, CultureInfo.InvariantCulture); } catch (FormatException e) { modelState.Errors.Add(e); } bindingContext.ModelState.Add(bindingContext.ModelName, modelState); return actualValue; } } 

如果我使用默认文化“en-US”这一切都没关系,但当我尝试将全球化设置为自动(这就是我需要的)时,我的文化是“es-ES”,当我写“3,2”时不要将我的字段检测为数字。 这里描述了问题和答案:使用由microsoft和模型绑定器在jquery中开发的插件。 模型绑定器不能解决我的问题,并且在Microsoft页面中链接的插件不起作用(断开链接)。 我能做什么?

更新:

我从nuget下载了Globalize并根据此链接添加了行(我不知道为什么stackoverflow不允许我复制该代码)

这将停用所有validation。

对于服务器端,您必须使用此模型绑定器:

 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (string.IsNullOrEmpty(valueResult.AttemptedValue)) { return 0m; } var modelState = new ModelState { Value = valueResult }; object actualValue = null; try { actualValue = Convert.ToDecimal( valueResult.AttemptedValue.Replace(",", "."), CultureInfo.InvariantCulture ); } catch (FormatException e) { modelState.Errors.Add(e); } bindingContext.ModelState.Add(bindingContext.ModelName, modelState); return actualValue; } 

并为客户端看看这个POST