1

Here is my DiscountRate variable in my model.

 public decimal DiscountRate { get; set; }

Here is my input in view.

 @Html.TextBoxFor(x=>x.DiscountRate, new {  @class = "form-control", @type = "number", step = "0.01", @placeholder = "0.00", @min="0", @max="100" })

The problem is when I enter a value (2,50 or 2.50 etc.) I catch it in my Controller like 250. What is the problem here?

5
  • Do you mean that when this form posts to the server, that is where the value is incorrect? Time to do some debugging then. In the browser's debugging tools, what is the resulting <input> element in the UI? In those same browser debugging tools, on the network tab, when the form is submitted to the server, what data does that request to the server contain? Is the value there correct? In the controller action receiving the request, what is that exact action and what is the exact model being supplied to that action? Commented Sep 30, 2024 at 14:22
  • Do you mean that on the same browser window, the server side sees 250 if you enter either 2.50 or 2,50? I would expect to have the issue with one input, whereas with the other input it would behave correctly. Commented Sep 30, 2024 at 14:30
  • Did you check the default regional settings? In different cultures, the ".", ",", "-", ":" are used differently and hence are also treated differently. For example "5.250.000,75" in German is written as "5,250,000.75" in English (the "decimal point is comma" problem). Commented Oct 1, 2024 at 6:20
  • Could you show the rest of your Model and View please? Also please show the Action in your Controller. Commented Oct 1, 2024 at 6:52
  • What's the culture you have used for your client? Commented Oct 1, 2024 at 8:19

1 Answer 1

1

Don't set @type as number, leave @type as text and use jQuery/javascript to make your control accept only numbers and decimal.

Setting @type as number is restricting textbox to accept only numbers as values or input.

Reference to allow numbers and decimals in textbox

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.