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>