0

I have a little problem. In ASP.NET Core, in view in form I want to enter double value with decimal point (in Czech with dot) and it doesn't want to take either one (for dot: the value '1.5' is not valid for... for comma: the field .... must be a number).

The value doesn't want to bind to the double property (same problem with decimal). There must be something basic that I'm overlooking.

My controller:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([Bind("Id,Name,Type,ApiId,Amount,InvestedMoney")] CommodityDto     commodity)
{
    if (ModelState.IsValid)
    {
        portfolioCommodityManager.Add(commodity);
        return RedirectToAction(nameof(Index));
    }

    return View(commodity);
}

My property:

public double Amount { get; set; }

My view:

<label asp-for="Amount" class="control-label fw-bold"></label>
<input asp-for="Amount" class="form-control" />
<span asp-validation-for="Amount" class="text-danger"></span>

This does not work:

services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new RequestCulture("cs-CZ");
});

---------

[DisplayFormat(DataFormatString = "{0:0.0}", ApplyFormatInEditMode = true)]
public double Amount { get; set; }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tsko
  • 33
  • 6

1 Answers1

0

You need to add this to your Program.cs:

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

Please note this will disable the other cultures likes en-US so you may want to add that too.

ctyar
  • 931
  • 2
  • 10
  • 22
  • Thanks, but that does not work too... :(, – Tsko Aug 06 '23 at 18:52
  • @Tsko I've tested this, and I'm sure it works, so the problem is something else. Be sure this is the first thing after `var app = builder.Build();` and let me know how it goes. – ctyar Aug 06 '23 at 18:59
  • If that doesn't resolve, I'll change all decimal and double models to string. This seems to be the most versatile solution ;) – Tsko Aug 06 '23 at 18:59
  • "Be sure this is the first thing after var app = builder.Build()" - still does not work... – Tsko Aug 06 '23 at 19:03
  • It works with ("en-US") settings. In CZ it won't let me a comma or a dot. (Validation rejects both, as I wrote) In the US, the number with a period works. But the string variant seems better to me, in the controller I convert it back to double and assign it to the model (And it doesn't matter if there is a dot or a comma) – Tsko Aug 06 '23 at 19:56
  • 1
    @Tsko I created a sample project here: https://github.com/ctyar/modelbindingsample It's just a form for a double property. If you send `1.2` it will show invalid, but if you send `1,2` it will accept it. – ctyar Aug 06 '23 at 19:57
  • Yes, in your project works all fine as you wrote... in my with 1,2 and ("cs-CZ") it shows "The field Amount must be a number." – Tsko Aug 06 '23 at 20:25
  • @Tsko Good that's progress. If you can post a more complete code, I might be able to find the issue. – ctyar Aug 06 '23 at 20:36
  • I could, but i think it is solved ;) but if you want, you can watch on my github [link](https://github.com/TomasSobotaT/Portfolio_Manager) – Tsko Aug 06 '23 at 20:40