0

Today I stumbled across a problem with the number input in a hmtl Form. I'm trying to get an Input from the User in form of a float. For this case i use the Input tag as following (Sorry for the English/German names):

<input asp-for="TBE_Leitwert" type="number" step="0.1" class="form-control" placeholder="Leitwert (mS)" /> 

Somehow after submitting the form I don't get the float value. It seems like the Input is ignoring the separator at all, Example:

The User puts in the value 1,1 but submitted was 11.

Value entered from a user:

Value entered from a user

Value from submitted the form:

Value from submitted form

The same problem results, even if I put the value 1.1 so it doesn't matter if I take the dot or comma, the value still is submitted wrong as 11

Is there any way to get the right float values from submitting the form without using JS or AJAX?

Edit: I made a super minimal reproducible exampele.

Index Page:

@model TestModel @{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <form asp-controller="Home" asp-action="GetValue"> <input asp-for="WantedValue" type="number" step="0.1" /> <button type="submit">Test</button> </form> </div> 

HomeController just added this 1 function:

public IActionResult GetValue(TestModel test) { return RedirectToAction("Index"); } 

Model:

public class TestModel { public double WantedValue { get; set; } } 

it is still the same, if i enter a float value on the index:

Value entered by User on Index

the Controller still gets a wrong value:

Wrong Value received from Form

since i entered "1,1" on the Index site i was expecting to get the value "1,1" in the controller. Same with the Value "1.1"

4
  • Please provide an minimal reproducible example
    – NineBerry
    CommentedOct 20, 2022 at 10:43
  • 2
    This is most likely a Culture problem. I suspect the view is running under either InvariantCulture or some US derived culture.CommentedOct 20, 2022 at 10:44
  • Does this answer your question? How to change symbol for decimal point in double.ToString()?CommentedOct 20, 2022 at 11:48
  • Sadly not, i cant change the input like in the given example because its already given by using html input type number. Also my problem occurs no matter which separator i use. I tried it with comma and dot, both gets "deleted" when submitted to controller.CommentedOct 20, 2022 at 12:06

2 Answers 2

1

You might need to set globalization culture in your project.

In asp.net core, add it in Startup.Configure:

var cultures = new[] { new CultureInfo("en-US"), new CultureInfo("de"), }; app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US"), SupportedCultures = cultures, SupportedUICultures = cultures }); 
1
  • I did implement your suggestion as you told me to. But sadly it still doesn't work. It is the same Problem as before. The Form Submitting 1,1 and 1.1 as 11. Nonetheless i'm very gratefull for your Help.CommentedOct 21, 2022 at 6:52
0

I know it's not the right way to solve the problem but it works. Try to use input type text instead of number like:

<input type="text" /> 

And then in the controller convert it to double in this way:

var number = collection["Number"]; var numberr = double.Parse(number.Replace('.', ',')); 

I hope this help you.

2
  • Thanks for your Help. This will work, but if the user enters something else than a number i run into a problem i guess. Maybe i'm also to picky, because i wanted to keep the nice error message of HTML bootstrap if the user enters an invalid number when try to submit.CommentedOct 21, 2022 at 13:20
  • I know that so it would be annoying to check that what the user entered contains only numbers inside the controller before redirecting the process to the same page to display an error message
    – Ali Hamed
    CommentedOct 21, 2022 at 13:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.