Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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#_.net_Asp.net Mvc 3_Decimal - Fatal编程技术网

C# 解析视图模型中的十进制数

C# 解析视图模型中的十进制数,c#,.net,asp.net-mvc-3,decimal,C#,.net,Asp.net Mvc 3,Decimal,我正在用ASP.NETMVC3开发一个网站 财产 [DisplayName("Cost"), DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)] public decimal Cost { get; set; } 看法 视图渲染成本为1000,00(例如)。问题是,验证需要一个点而不是逗号。如何输出1000.00而不是1000,00?或者反向验证以接受逗号而不是点 编辑。我已经在我的web.conf

我正在用ASP.NETMVC3开发一个网站

财产

[DisplayName("Cost"), DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
public decimal Cost { get; set; }
看法

视图渲染成本为1000,00(例如)。问题是,验证需要一个点而不是逗号。如何输出1000.00而不是1000,00?或者反向验证以接受逗号而不是点


编辑。我已经在我的web.config中将全球化设置为sv SE(瑞典)。

您是否可以更改DataFormatString,以便它使用点(例如,{0:0.00}或类似点)对数字进行格式化?

我的国家也存在解析小数分隔符的问题是逗号:

我发现有些变通方法不太好:


要做到这一点,您需要编写一个自定义模型活页夹

/// <summary>
/// http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx
/// </summary>
public class DecimalModelBinder : 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.ToDecimal(valueResult.AttemptedValue,
                CultureInfo.CurrentCulture);
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}

格式化这不是问题,是验证会失败。谢谢snipet!工作起来很有魅力。我在使用与小数相对的双精度值时遇到了问题,并且使用了[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:#,0}”)]属性,该属性将双精度值格式化为不带小数的货币。虽然框架在默认情况下没有处理这些问题,但感觉有点傻。
/// <summary>
/// http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx
/// </summary>
public class DecimalModelBinder : 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.ToDecimal(valueResult.AttemptedValue,
                CultureInfo.CurrentCulture);
        }
        catch (FormatException e)
        {
            modelState.Errors.Add(e);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());