-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Localised number format and wrong input type for decimal fields #6566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ran into the same bug and helped me out with a custom TagHelper that recognizes the
You also might want to set the "step" attrbute to "any". For the .NET Core MVC Team: Lines found:
|
Thanks 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. |
The latter error is caused because you are using jquery-validation. It has a function that validates number, it has nothing to do with browser language. You can test it here https://jqueryvalidation.org/number-method/. To get comma validated as a decimal separator, you can override the jquery-validation number validation as suggeted by @Nikkelmann #6566 (comment) |
According to HTML standard, the 'value' of a number input element should always be in the invariant culture. Browsers may choose to render it differently based on the users browser settings. From what I remember this has been a source of pain since Webforms / MVC 1. |
I'm tired of searching for a permanent solution to this problem in every new project for years. Can you please solve this problem in 7.0-rc1 milestone? |
Background and Motivation@MackinnonBuck writes,
Proposed APInamespace Microsoft.AspNetCore.Mvc.ViewFeatures;
public class HtmlHelperOptions
{
+ public bool SuppressCultureInvariantFormValueFormatting { get; set; }
}
namespace Microsoft.AspNetCore.Mvc.Rendering;
public class ViewContext
{
+ public bool SuppressCultureInvariantFormValueFormatting { get; set; }
}
Usage Examplespublic void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddViewOptions(options =>
{
options.HtmlHelperOptions.SuppressCultureInvariantFormValueFormatting = true;
});
} Alternative Designs
RisksN/A |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
@captainsafia writes,
|
How likely do we think it is that people will want to change this? Given the description of the PR it seems like we're just correcting behavior but we're adding an escape hatch out of an abundance of caution. Can we make this an AppContext switch instead of a public API? |
I like this name.
It looks like we've already used |
Adding on to my previous comment, |
I do think there's merit to adding options to
I like that idea. What would you name the enum? By the way, how does this new API interact with the existing Also, can you explain how the |
Proposed APInamespace Microsoft.AspNetCore.Mvc.Rendering;
+public enum FormInputRenderMode
+{
+ DetectCultureFromInputType = 0,
+ AlwaysUseCurrentCulture = 1,
+}
namespace Microsoft.AspNetCore.Mvc.ViewFeatures;
public class HtmlHelperOptions
{
+ public FormInputRenderMode FormInputRenderMode { get; set; }
} Usage Examplespublic void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddViewOptions(options =>
{
options.HtmlHelperOptions.FormInputRenderMode = FormInputRenderMode.AlwaysUseCurrentCulture;
});
} |
It does not interact with other properties - it's completely independent.
Presumably it's because properties on |
The above API makes sense to me. Do we prefer "render" mode over "rendering" mode? |
I think
I've chosen And I think |
Hm, I really don't have a preference here. Maybe I like "render" a bit more just because the "ing" doesn't really add any important information. Totally willing to change my mind though! |
If there are no objections, I can mark this |
As I understood it, the new default will be the fixed |
API review notes:
API Approved! |
Hi, |
Describe the bug
With a model that contains a
decimal
property, this doesn't work with a locale that uses a decimal comma:To Reproduce
Steps to reproduce the behavior:
Code:
Expected behavior
This should be the rendered HTML:
This happens instead, for the two view variants from the top:
The first issue is that the
decimal
type isn't rendered with thetype="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]
The text was updated successfully, but these errors were encountered: