ASP.NET Core本地化十进制字段点和逗号 [英] ASP.NET Core localization decimal field dot and comma

查看:133
本文介绍了ASP.NET Core本地化十进制字段点和逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地化的ASP.NET Core Web应用程序:en-US和it-IT.

I have a localized ASP.NET Core Web Application: en-US and it-IT.

在美国,小数点分隔符是点,在IT中,小数点分隔符是逗号.

On en-US the decimal separator is dot, in it-IT the decimal separator is comma.

我有这个ViewModel

I have this ViewModel

public class MyViewModel 
{
    public int Id {get; set; }

    // Omitted

    public decimal? Amount{get; set;}

}

对于当我在 zh-CN html文本框呈现

For the decimal field when I render the create/edit page on en-US the html textbox render

1000.00

1000.00

如果我过帐表格,则操作完成而没有错误.

If I POST the form, the operation complete without errors.

到目前为止一切都很好.

So far so good.

当我在 it-IT 上呈现html文本框呈现时创建/编辑页面

When I render the create/edit page on it-IT the html textbox render

1000,00(注意逗号)

1000,00 (notice the comma)

如果我尝试过帐表单,(客户)验证失败

And IF I try to POST the form, (CLIENT) validation fail with

金额字段必须是数字.

The field Amount must be a number.

我了解了有关IModelBinder的信息,但我知道这是在将表单发布到服务器上时映射viewModel的情况,因为我被客户端验证阻止了.

I read about the IModelBinder but I understand is for mapping the viewModel when the form is posted on the server, on my case I'm blocked by the client-side validation.

更好的方法是在en-US时使用点,在IT时使用逗号,但是只使用点即可.

The better is to use dot when en-US and comma when it-IT, but it's fine using only the dot

推荐答案

在深入研究问题之后,我找到了两种解决方案:

After digging depth the problem I found two solution:

来自 Stephen Muecke 的评论,其中说明了如何将必需的jquery添加到对逗号和点进行验证

The comment from Stephen Muecke where explain how to add the required jquery to the input for a validation for comma and dot

自定义InputTagHelper,将逗号转换为点.在这里,我仅添加了一个十进制类型,但显然您可以添加float和double.

A custom InputTagHelper where transform the comma into dot. Here I added only a decimal type but obviously you can add float and double.

[HtmlTargetElement("input", Attributes = ForAttributeName, TagStructure = TagStructure.WithoutEndTag)]
public class InvariantDecimalTagHelper : InputTagHelper
{
    private const string ForAttributeName = "asp-for";

    private IHtmlGenerator _generator;

    [HtmlAttributeName("asp-is-invariant")]
    public bool IsInvariant { set; get; }

    public InvariantDecimalTagHelper(IHtmlGenerator generator) : base(generator)
    {
        _generator = generator;
    }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        base.Process(context, output);

        if (IsInvariant && output.TagName == "input" && For.Model != null && For.Model.GetType() == typeof(decimal))
        {
            decimal value = (decimal)(For.Model);
            var invariantValue = value.ToString(System.Globalization.CultureInfo.InvariantCulture);
            output.Attributes.SetAttribute(new TagHelperAttribute("value", invariantValue));                
        }
    }
}

要使用第二种解决方案,只需将asp-is-invariant添加到您的输入中,就像这样

For use this 2nd solution you simply add asp-is-invariant to your input, like this

 <input asp-for="AmountSw" class="form-control" asp-is-invariant="true" />

这篇关于ASP.NET Core本地化十进制字段点和逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆