如何在ASP.NET MVC控制器中设置小数分隔符?

Paw*_*iak 20 globalization asp.net-mvc

我正在使用NerdDinner应用程序试图自学ASP.NET MVC.但是,我偶然发现全球化问题,我的服务器使用逗号作为小数分隔符显示浮点数,但虚拟地球地图需要点数,这会导致一些问题.

我已经在我的视图中解决了映射JavaScript的问题,但是如果我现在尝试发布带有点作为十进制分隔符的已编辑的晚餐条目,则控制器InvalidOperationException在更新模型时失败(抛出)(在UpdateModel()metod中).我觉得我必须在控制器的某个地方设置适当的文化,我尝试了OnActionExecuting()但是没有帮助.

Paw*_*iak 60

我刚刚在一个真实的项目中重新审视了这个问题,最后找到了一个有效的解决方案.正确的解决方案是为该类型设置一个自定义模型绑定器decimal(decimal?如果您正在使用它们):

using System.Globalization;
using System.Web.Mvc;

public class DecimalModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        object result = null;

        // Don't do this here!
        // It might do bindingContext.ModelState.AddModelError
        // and there is no RemoveModelError!
        // 
        // result = base.BindModel(controllerContext, bindingContext);

        string modelName = bindingContext.ModelName;
        string attemptedValue = bindingContext.ValueProvider.GetValue(modelName)?.AttemptedValue;

        // in decimal? binding attemptedValue can be Null
        if (attemptedValue != null)
        {
            // Depending on CultureInfo, the NumberDecimalSeparator can be "," or "."
            // Both "." and "," should be accepted, but aren't.
            string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
            string alternateSeperator = (wantedSeperator == "," ? "." : ",");

            if (attemptedValue.IndexOf(wantedSeperator, StringComparison.Ordinal) == -1
                && attemptedValue.IndexOf(alternateSeperator, StringComparison.Ordinal) != -1)
            {
                attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator);
            }

            try
            {
                if (bindingContext.ModelMetadata.IsNullableValueType && string.IsNullOrWhiteSpace(attemptedValue))
                {
                    return null;
                }

                result = decimal.Parse(attemptedValue, NumberStyles.Any);
            }
            catch (FormatException e)
            {
                bindingContext.ModelState.AddModelError(modelName, e);
            }
        }

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在Application_Start()中的Global.asax.cs中:

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());
Run Code Online (Sandbox Code Playgroud)

请注意,代码不是我的,我实际上是在Kristof Neirynck的博客上找到的.我刚编辑了几行,并为特定数据类型添加了binder,而不是替换默认的binder.

  • 我刚刚更新了我的答案,添加了一个额外的代码检查,以使其正确处理可空属性.我发现,如果它发现一个空字符串作为尝试的值,即使绑定的属性可以为空,它也会抛出异常.现在它应该工作得很好. (2认同)

Nic*_*rdi 5

在web.config中设置它

  <system.web>
    <globalization uiCulture="en" culture="en-US" />
Run Code Online (Sandbox Code Playgroud)

您似乎正在使用使用逗号而不是小数位的语言设置的服务器.您可以将文化调整为使用逗号设置的文化,例如en-US.