When I submit the form, the animal weight entered as a decimal is posted as a whole number.
4.5 or 4,5 comes to model as 45.
What can I try next?
This is the entity class:
public partial class animal
{
public double animalWeight { get; set; }
}
And this is the view:
<label asp-for="animalWeight" class="row-sm-2 col-form-label">Animal Weight</label>
<input type="number" asp-for="HayvanKilo" class="form-control" placeholder="Animal Weight" step="0.1">
</input>
<div class="mt-2">
<span class="font-weight-bold text-danger bg-light text-danger rounded" asp-validation-for="animalWeight">
</span>
</div>
When I submit the form, the animal weight entered as a decimal is posted as a whole number.
4.5 or 4,5 comes to model as 45.
Based on your shared code, I founded few inconsistencies which might be causing why the whole number is posting.
First of all, your shared code shows that your property name is animalWeight
within the animal
class. However, from your view you are passing asp-for="HayvanKilo"
which is incorrect. I don't know if that's typos or not.
Apart from that, some browser could also cause that issue because of culture settings
I have tested as following and got the expected output you are looking for.
Let's have a look in practice:
Class:
public partial class Animal
{
public decimal AnimalWeight { get; set; }
}
Controller:
public class DecimalController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(Animal animal)
{
ViewBag.Message = $"Animal Weight: {animal.AnimalWeight}";
return View(animal);
}
}
View:
@model CustomerManagementTool.Models.Animal
<h2>Enter Animal Weight</h2>
<form asp-action="Index" method="post">
<div class="form-group">
<label asp-for="AnimalWeight" class="control-label">Animal Weight</label>
<input type="number" asp-for="AnimalWeight" class="form-control" step="0.1" />
<span asp-validation-for="AnimalWeight" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Output:
Note: It may be worth checking the property and CultureInfo.InvariantCulture settings. In addition, you could refer to this thread as well.