22

ASP.NET MVC 中的模型绑定很棒,但它遵循区域设置。在我的语言环境中,小数点分隔符是逗号 (','),但用户也使用点 ('.'),因为他们懒得切换布局。我希望在一个地方为我的模型中的所有decimal字段实现这一点。

我应该为类型实现我自己的值提供者(或事件模型绑定器)decimal还是我错过了一些简单的方法来做到这一点?

4

4 回答 4

41

最干净的方法是实现自己的模型绑定器

public class DecimalModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue);
        // of course replace with your custom conversion logic
    }    
}

并在 Application_Start() 中注册它:

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

致谢:默认 ASP.NET MVC 3 模型绑定器不绑定小数属性

于 2013-01-18T14:14:44.240 回答
5

要正确处理组分隔符,只需替换

Convert.ToDecimal(valueProviderResult.AttemptedValue);

在选定的答案中

Decimal.Parse(valueProviderResult.AttemptedValue, NumberStyles.Currency);
于 2015-06-23T22:58:10.833 回答
4

感谢接受的答案,我最终得到了以下实现来处理浮点数、双精度和十进制。

public abstract class FloatingPointModelBinderBase<T> : DefaultModelBinder
{
    protected abstract Func<string, IFormatProvider, T> ConvertFunc { get; }

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (valueProviderResult == null) return base.BindModel(controllerContext, bindingContext);
        try
        {
            return ConvertFunc.Invoke(valueProviderResult.AttemptedValue, CultureInfo.CurrentUICulture);
        }
        catch (FormatException)
        {
            // If format error then fallback to InvariantCulture instead of current UI culture
            return ConvertFunc.Invoke(valueProviderResult.AttemptedValue, CultureInfo.InvariantCulture);
        }
    }
}

public class DecimalModelBinder : FloatingPointModelBinderBase<decimal>
{
    protected override Func<string, IFormatProvider, decimal> ConvertFunc => Convert.ToDecimal;
}

public class DoubleModelBinder : FloatingPointModelBinderBase<double>
{
    protected override Func<string, IFormatProvider, double> ConvertFunc => Convert.ToDouble;
}

public class SingleModelBinder : FloatingPointModelBinderBase<float>
{
    protected override Func<string, IFormatProvider, float> ConvertFunc => Convert.ToSingle;
}

然后你只需要在Application_Start方法上设置你的 ModelBinders

ModelBinders.Binders[typeof(float)] = new SingleModelBinder();
ModelBinders.Binders[typeof(double)] = new DoubleModelBinder();
ModelBinders.Binders[typeof(decimal)] = new DecimalModelBinder();
于 2017-03-01T16:07:00.947 回答
2
var nfInfo = new System.Globalization.CultureInfo(lang, false)
{
    NumberFormat =
    {
        NumberDecimalSeparator = "."
    }
};
Thread.CurrentThread.CurrentCulture = nfInfo;
Thread.CurrentThread.CurrentUICulture = nfInfo;
于 2014-10-15T10:26:36.910 回答