首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ASP.NET控件中设置小数点分隔符?

如何在ASP.NET控件中设置小数点分隔符?
EN

Stack Overflow用户
提问于 2009-04-27 13:21:50
回答 2查看 29.4K关注 0票数 23

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

我已经解决了the issue with the mapping JavaScript in my views,但如果我现在尝试发布一个编辑过的晚餐条目,并将点作为小数点分隔符,则当更新模型(在UpdateModel()方法中)时,控制器将失败(抛出InvalidOperationException)。我觉得我也必须在控制器中的某个地方设置适当的区域性,我在OnActionExecuting()中尝试了一下,但这没有帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-25 20:51:14

我刚刚在一个真实的项目中重新考虑了这个问题,并最终找到了一个可行的解决方案。正确的解决方案是为类型decimal (以及decimal?,如果您正在使用它们)提供一个自定义模型绑定:

代码语言:javascript
复制
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;
    }
}

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

代码语言:javascript
复制
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());

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

票数 65
EN

Stack Overflow用户

发布于 2009-04-27 13:32:23

在你的web.config中设置这个

代码语言:javascript
复制
  <system.web>
    <globalization uiCulture="en" culture="en-US" />

您使用的服务器似乎安装了使用逗号而不是小数位的语言。您可以将区域性调整为以设计应用程序的方式使用逗号的区域性,例如en-US。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/793459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档