With a model that contains a decimal
property, this doesn't work with a locale that uses a decimal comma:
<input asp-for="Number">
<input asp-for="Number" type="number">
Steps to reproduce the behavior:
Code:
class Model
{
public decimal Number { get; set; } = 1.0m;
}
This should be the rendered HTML:
<input type="number" name="Number" value="1.0">
This happens instead, for the two view variants from the top:
<input type="text" name="Number" value="1,0">
<input type="number" name="Number" value="1,0">
The first issue is that the decimal
type isn't rendered with the type="number"
attribute. If I fix this myself by adding the attribute, the field remains completely empty because there is a comma instead of a point in the value. This shouldn't be localised because then no browser or frontend library can handle the value anymore.
PS: I haven't even got to try what happens when the correct value "1.0" is sent back to the controller and the model binder should convert it to a decimal
type. It probably fails for the same reason.
[User reference: Configuration/Endpoint/Edit/Factor]
Ran into the same bug and helped me out with a custom TagHelper that recognizes the For.ModelExplorer.ModelType
as decimal or nullable decimal and then modifies the value attribute.
```
if (For.Model != null)
{
decimal val = (decimal)For.Model;
string value = val.ToString(CultureInfo.InvariantCulture);
output.Attributes.SetAttribute(new TagHelperAttribute("value", value));
}
````
You also might want to set the "step" attrbute to "any".
For the .NET Core MVC Team:
I found some lines that are part of the bug, but I was not able to solve the wohle problem.
Lines found:
CultureInfo.CurrentCulture
(for me, its de-DE!) as value. CultureInfo.InvariantCulture
is needed to parse correctly: https://github.com/aspnet/AspNetCore/blob/1aa50faa290ecda304507981cd01ed92651d5e34/src/Mvc/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/DecimalModelBinder.cs#L80Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.
@mkArtakMSFT Can you please explain why you believe the question has been answered / the bug has been fixed? I cannot see how this is the case. Please reopen until this has been fixed. In case you didn't know, there are other regions in the world than those that use a decimal point. And currently I can't use proper HTML with decimal numbers with ASP.NET Core MVC data binding.
Looks like I misread this. Reopening for further investigation.
@mkArtakMSFT Did you want to reopen this issue?
type="number" fields use invariant culture and we should consider parsing as such in model binding.
Any progress on this? I hit the same behavior with Blazor WebAssembly though I think it also applies to Blazor Server Side. The same applies to the min, max and step attribute of an input-tag of type number or range.
Experiencing the same issue on latest 3.1 stream of .NET Core. I have German culture, so we use "," as decimal separator. When I send a decimal value from an input field to the controller, the value "214,50" is sent to the controller. But when mapped to my ViewModel in the controller, the comma is lost and the value 21450 in mapped to my models attribute which is a decimal type...
However, with a piece of old legacy code within the same solution, the mapping works just fine although the setup is exactly the same.
EDIT: I just found out that the error only occurs when I use GET. When I send the data via POST, the binding works just fine.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
@mkArtakMSFT Thanks for the update. I'd suggest you put a bold warning in the ASP.NET Core documentation then which says that this product does not support other server environments than English (and few more) and will not for an unspecified time. At least everybody knows what to expect then, and when to look for alternatives.
Does the Fix from @AndreasAmMueller work ?
I am considering removing type number but If it can be fixed like this then I would do it like him. Are there other problems I may run into?
I am having same problem:
I have this Entity:
namespace Backend.Models
{
public class Tax
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Column(TypeName = "decimal(10,3)")]
public decimal Value { get; set;}
}
}
When I use scaffold Razor Page using Entity Framework (CRUD) and execute the solution I have this html error:
Location in spanish, decimal is comma separated, because html throw this error saying is not a number.
Also, if I use dot decimal separator the database save a number without decimals:
驴This can be related to this issue?
驴How can I solve this issue?
Thanks in advance.
Most helpful comment
@mkArtakMSFT Can you please explain why you believe the question has been answered / the bug has been fixed? I cannot see how this is the case. Please reopen until this has been fixed. In case you didn't know, there are other regions in the world than those that use a decimal point. And currently I can't use proper HTML with decimal numbers with ASP.NET Core MVC data binding.