1

I'm facing a very weird problem, every time I update my data (without changing the price) the price will be updated as well (1200,00 => 120000,00). Is there any solution to this? The controller and view are built using the scaffold.

I'm using custom tag helper (asp-for-invariant="Price") from ASP.NET Core Localization Decimal Field Dot and Coma. I have noticed that with or without a custom tag helper the weird problem still occurs.

Here is my model

[Required]
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }

Here is my controller (edit)

public async Task<IActionResult> Edit(int id, [Bind("AlbumId,GenreId,ArtistId,Title,Price,ImageFile")] Album album)
{
    System.Diagnostics.Debug.WriteLine(album.ImageFile != null);

    if (id != album.AlbumId)
    {
        return NotFound();
    }

    if (ModelState.IsValid)
    {
        try
        {
            if (album.ImageFile != null)
            {
                if (album.ImageName != null)
                {
                    // delete old image
                    DeleteImage(ImagePathGenerator(album.ImageName));
                } 

                // save new image
                album.ImageName = ImageNameGenerator(album.ImageFile.FileName);
                string path = ImagePathGenerator(album.ImageName);

                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await album.ImageFile.CopyToAsync(fileStream);
                }
            }

            _context.Albums.Update(album);
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!AlbumExists(album.AlbumId))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return RedirectToAction(nameof(Index));
    }

    ViewData["ArtistId"] = new SelectList(_context.Artists, nameof(Artist.ArtistId), nameof(Artist.Name));
    ViewData["GenreId"] = new SelectList(_context.Genres, nameof(Genre.GenreId), nameof(Genre.Name));

    return View(album);
}

Here is my edit.cshtml

 <div class="form-group">
      <label asp-for="Price" class="control-label"></label>
      <input class="form-control" asp-for-invariant="Price" />
      <span asp-validation-for="Price" class="text-danger"></span>
 </div>

Here is my index.cshtml

<td>
     @Html.DisplayFor(modelItem => item.Price)
</td>

Data table

Edit data

Data table 2

  • Can you include the code of both the views as well? – Amal K Jun 02 '21 at 08:05
  • 1
    @AmalK Hi, I have updated the post – Sherwin Variancia Jun 02 '21 at 08:38
  • Did you try placing a breakpoint on the controller action and checking the value of `album.Price` or perhaps checking the database to see if the value is really updated or is it just being displayed like that? – Amal K Jun 02 '21 at 09:23
  • @AmalK the database was updated – Sherwin Variancia Jun 02 '21 at 10:01
  • Install [cldr js libraries](https://github.com/unicode-org/cldr) to have full support for client-side validation of localized inputs like decimal numbers, dates, etc.. see this post for more details: http://ziyad.info/en/articles/35-How_to_install_client_side_validation_scripts – LazZiya Jun 06 '21 at 07:37

2 Answers2

2

In your GUI, type 1200.00 causes the value 120000 . Check your Region settings, or try 1200 or 1200,00 .

enter image description here

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Wow, it works! But doesn't make any sense if every user in the future needs to go to the control panel and set this up. Is there a way to overcome this problem? – Sherwin Variancia Jun 02 '21 at 15:45
  • See another solution at https://stackoverflow.com/a/67807514/3728901 . Let's me know `asp-is-invariant="false"` or `asp-is-invariant="true"` help you? – Vy Do Jun 02 '21 at 15:53
0

Simple solution, try

<input class="form-control" asp-for-invariant="Price" asp-is-invariant="false"/>

or

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

Set culture https://stackoverflow.com/a/8744037/3728901

Vy Do
  • 46,709
  • 59
  • 215
  • 313