Answer the question
In order to leave comments, you need to log in
How to insert values with dot or comma in ASP.NET Core 2.0?
I think that my question is the top one for today.
https://github.com/dignatukhin/albeor1.git
Microsoft is a top company you say? well, yes, they also have top encoders (ASP.NET Core 2.0). Watafak, how to fix it?
In the model:
[Required]
public decimal Price { get; set; }
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,ReleaseDate,Genre,Price, Rating")] Movie movie)
{
if (ModelState.IsValid)
{
_context.Add(movie);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(movie);
}
<div class="form-group">
<label asp-for="Price" class="control-label"></label>
<input asp-for="Price" class="form-control" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
Answer the question
In order to leave comments, you need to log in
1) Try float or double
2) For input add type="text"
UPD
This is how it works if you enter it separated by commas
[DataType(DataType.Currency, ErrorMessage = "Must be a Deciaml!")]
public float Price { get; set; }
The fact that you marked a field as [Required] only means that this field must be filled in. However, you did not specify the check logic. It's Microsoft's fault...
AleXr64 on GitHub suggested adding [RegularExpression(@"^\d+.?\d{0,2}$", ErrorMessage = "Invalid Target Price; Maximum Two Decimal Points.")]
Maybe someone is interested, but this one option didn't work either.
If you need to bind a floating point value, then this is how you can:
https://github.com/badhitman/AspDotNetCore2BinderF...
It will not work for client-side validation for JS, but the server will work correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question