The problem:
When the information of the form is sent to the POST Action, the float variables lose their decimal point and gets converted to an integer
instead of a float
.
Information:
This is the class that contains the definition of the variables: PUnit
and CoinValue
:
public class Device
{
[Key]
public int DeviceID { get; set; }
[Display(Name = "Precio por Jugada")]
public float PUnit { get; set; }
public float CoinValue { get; set; }
}
In the view I present a collection of this class, each one wrapped inside a form in order to be sent to the Controller indiviadually:
@model IEnumerable<Application.Models.Device>
{...}
<div class="form-group" form="@(String.Format("{0}{1}", "form", item.DeviceID))">
<div>
<input type="number" name="CoinValue" asp-for="@item.CoinValue" min="0" max="5" step="0.5"
form="@(String.Format("{0}{1}", "form", item.DeviceID))" class="form-control" />
<span asp-validation-for="@item.CoinValue" class="text-danger"></span>
</div>
</div>
In the POST Action, the information of the Device
gets saved:
[HttpPost, ActionName("Contadores")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Contadores(int? id)
{
var contadortoupdate = _context.Devices.SingleOrDefault(c => c.DeviceID == id.Value);
if (await TryUpdateModelAsync(contadortoupdate, "",
c => c.DeviceStatus, c => c.InitialAmount, c=>c.CoinValue, c => c.PUnit, c => c.SellType))
{
await _context.SaveChangesAsync();
return RedirectToAction("Contadores");
}
return RedirectToAction("Contadores");
}
Results:
When I insert a value of 0,5
, for example, it gets converted and saved into the Database as 5. Same as 4,5; it's shown as 45 and I don't know why is that. Thanks for any advice.
UPDATE:
After debugging with Chrome I found out that the numbers in the form were being sent with this forma: 1.5
instead of 1,5
which causes the error. This ocurrs only in this View since I tested it on another one and it didn't change the format of the number.
The difference between those two forms is that the second one is defined just as an input:
<div class="form-group">
<label asp-for="StoreArea" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="StoreArea" class="form-control" />
<span asp-validation-for="StoreArea" class="text-danger"></span>
</div>
</div>
While the first one is defined as
<input type="number" {...}>
I believe that adding this ´type="number"´ property is the one that is changing the format of the input but is my database who is not accepting this value as valid, I believe.