繁体   English   中英

ASP.NET MVC 5应用程序中的十进制数字

[英]Decimal numbers in ASP.NET MVC 5 app

我有十进制数问题。

如果我在文本框中使用。(点)而不是(逗号),它在控制器中为空。

我知道它是一个语言问题,因为在西班牙语中我们使用逗号而不是小数点,但我需要使用点。

有可能改变这个吗?

这很奇怪,因为在控制器中我必须使用。(点)表示小数,即:

我可以做float x = 3.14但我不能做float x = 3,14所以我不明白这...在某些情况下我必须使用点...在其他情况下我必须使用逗号...

这是我的代码:

在模型中:

[Display(Name = "Total")]
public double Total { get; set; }

在视图中:

@Html.EditorFor(model => model.Total, new { id = "Total", htmlAttributes = new {@class = "form-control" } })

在控制器中:

public ActionResult Create([Bind(Include = "ID,Codigo,Fecha,Trabajo,Notas,BaseImponible,Iva,Total,Verificado,FormaDePagoID,ClienteID")] Presupuesto presupuesto)
    {

谢谢大家。 我发现这个来自Phil Haack的代码效果非常好。

在项目的任何文件夹中创建一个类

public class ModelBinder
{
    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;

            // 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) == -1
                && attemptedValue.IndexOf(alternateSeperator) != -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;
        }
    }
}

将其添加到Global.asax中的Application_Start()方法

    ModelBinders.Binders.Add(typeof(decimal), new ModelBinder.DecimalModelBinder());
    ModelBinders.Binders.Add(typeof(decimal?), new ModelBinder.DecimalModelBinder());

现在使用十进制类型而不是浮点数或双倍,一切都会好起来!! 谢谢伙伴见到你!

您的控制器使用C#。 语言具体说明. 是小数点分隔符。 期。 这不是语言特定的,就是这样。

您的数据库或UI(使用服务器的语言设置)可能使用另一个小数分隔符,而不是C#使用的默认(美国)语言设置。 这就是为什么你必须使用,作为分隔符。

您需要使用自定义模型绑定器。

请参阅此博客文章http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx/

如果你希望你的逗号(,)根据UI文化在UI中分隔十进制输入,要转换为点(。)以绑定到C#十进制数,你可以去Asp.Net MVC的自定义模型绑定器,其中以逗号分隔十进制字符串并用点替换逗号,然后分配给C#decimal属性。

优点是,它可以在整个应用程序中重复使用,您可能会在其中重复使用十进制转换方案。

希望以下链接可以帮助您:

ASP.Net MVC自定义模型绑定说明 http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx http:// haacked的.com /存档/ 2011/03/19 /固定结合到decimals.aspx /

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM