0

I have the known problem on decimal data types and the error "The field fieldname must be a number." I'm developing a ASP.NET Web App in .NET Core 2.2 with c#.

The model extract is the following:

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

and the cshtml extract is as follows:

    <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>

After enabling globalization for jquery validation plugin and putting the following code in 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);

the problem persist.

Any suggestions? Thanks.

Simone Spagna
  • 626
  • 7
  • 27
  • 2
    What known problem? Where did this error come from? Compilation? Runtime? Javascript? Are you sure it's not caused by invalid input, or a globalization issue? – Panagiotis Kanavos Sep 26 '19 at 10:24
  • @PanagiotisKanavos : https://stackoverflow.com/questions/11822480/error-with-decimal-in-mvc3-the-value-is-not-valid-for-field/37135252#37135252 – Simone Spagna Sep 26 '19 at 10:27
  • 1
    So you know this is a *globalization issue*, the input is invalid and you need to specify the correct culture, not disable validation. – Panagiotis Kanavos Sep 26 '19 at 10:29
  • @PanagiotisKanavos : So I have to fix a culure with a specified decimal point for rhe application. – Simone Spagna Sep 26 '19 at 10:31
  • 1
    ASP.NET Core uses the [jQuery validation plugin](https://jqueryvalidation.org/). You need to enable globalization for the . This is shown [here](https://github.com/aspnet/AspNetCore.Docs/issues/4076#issuecomment-326590420) and [here](https://www.codeproject.com/Articles/1212247/jQuery-culture-validation-in-ASP-NET-Core). – Panagiotis Kanavos Sep 26 '19 at 10:36
  • @PanagiotisKanavos : Hi, I did everything you suggested, but the problem persists. What can it be? – Simone Spagna Sep 26 '19 at 11:36
  • 1
    What you want is to make the validation message appear as the language of the corresponding culture ? – Xueli Chen Sep 27 '19 at 08:55

1 Answers1

2

If you want to localize ASP.NET Core Model Binding Error Messages ,follow these steps :

  1. Create Resource File - Create a resource file under Resources folder in your solution and name the file ModelBindingMessages.fa.resx.

  2. Add Resource Keys - Open the resource file and add keys and values which you want to use for localizing error messages. I used keys and values like below image: enter image description here

  3. Configure Options - In ConfigureServices method, when adding Mvc, configure its options to set message accessors for 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);
     }
    

    The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the Startup.Configure method. The localization middleware must be configured before any middleware which might check the request culture (for example, 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
        });
    

Reference :

https://learn.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

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36