【问题标题】:The decimal field must be a number in .net core 2.2小数字段必须是 .net core 2.2 中的数字
【发布时间】:2019-09-26 10:18:25
【问题描述】:

我有十进制数据类型的已知问题和错误“字段字段名必须是数字”。我正在使用 c# 在 .NET Core 2.2 中开发 ASP.NET Web 应用程序。

模型摘录如下:

    public DateTime? ValidTo { get; set; }
    public decimal? TimeZone { get; set; }
    public int? Idwfstate { get; set; }

而cshtml提取如下:

    <div class="form-group">
            <label asp-for="item.TimeZone" class="control-label"></label>
            <input asp-for="item.TimeZone" class="form-control" />
            <span asp-validation-for="item.TimeZone" class="text-danger"></span>
    </div>

为 jquery 验证插件启用全球化并将以下代码放入 startup.cs 后:

        var defaultCulture = new CultureInfo("us-UK");
        var localizationOptions = new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture(defaultCulture),
            SupportedCultures = new List<CultureInfo> { defaultCulture },
            SupportedUICultures = new List<CultureInfo> { defaultCulture }
        };
        app.UseRequestLocalization(localizationOptions);

问题仍然存在。

有什么建议吗?谢谢。

【问题讨论】:

  • 什么已知问题?这个错误是从哪里来的?汇编?运行? Javascript?确定不是无效输入,还是全球化问题?
  • 所以您知道这是一个全球化问题,输入无效,您需要指定正确的文化,而不是禁用验证。
  • ASP.NET Core 使用jQuery validation plugin。您需要为 .这显示为herehere
  • 您想要的是将验证消息显示为相应文化的语言?

标签: c# validation asp.net-core


【解决方案1】:

如果您想本地化 ASP.NET Core 模型绑定错误消息,请按照以下步骤操作:

  1. 创建资源文件 - 在Resources 文件夹下创建资源文件 在您的解决方案中并将文件命名为ModelBindingMessages.fa.resx

  2. 添加资源键 - 打开资源文件并添加键和值 你想用它来本地化错误消息。我用钥匙和 如下图所示的值:

  3. 配置选项 - 在ConfigureServices方法中,添加Mvc时,配置其选项为ModelBindingMessageProvider设置消息访问器:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(options => { options.ResourcesPath = "Resources";});
    
        services.AddMvc(options =>
        {
            var F = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
            var L = F.Create("ModelBindingMessages", "RazorPages2_2Test");
            options.ModelBindingMessageProvider.SetValueIsInvalidAccessor(
                (x) => L["The value '{0}' is invalid."]);
            options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor (
                (x) => L["The field {0} must be a number."]);
    
        } )
            .AddDataAnnotationsLocalization()
            .AddViewLocalization()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
     }
    

    请求的当前文化在本地化中间件中设置。本地化中间件在Startup.Configure 方法中启用。必须在任何可能检查请求文化的中间件之前配置本地化中间件(例如,app.UseMvcWithDefaultRoute())。

       var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("fa") };
        app.UseRequestLocalization(new RequestLocalizationOptions()
        {
            DefaultRequestCulture = new RequestCulture(new CultureInfo("en")),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        });
    

参考:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2#implement-a-strategy-to-select-the-languageculture-for-each-request

https://stackoverflow.com/a/41669552/10201850

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多