1

I have an input for decimal values in HTML:

<input type="number" step="0.01"/>

My model and my variables in C# are declared as decimal as well. But when I, for example, input a number such as 0,15 I get the value of 15 instead of the original 0,15 on my Controller.

I'm posting the data using the form method="post" from HTML

The model is:

public decimal Value { get; set; }

What can I do to receive 0.15 as the value in my controller?

  • 2
    how are you posting the data to server, is it an ajax request ? Can you provide the model class? – Vishnu Apr 17 '19 at 18:26
  • use this link: https://stackoverflow.com/questions/32908503/c-sharp-mvc-controller-cannot-get-decimal-or-double-values-from-ajax-post-reques – hassan.ef Apr 17 '19 at 18:27
  • @Vishnu I'm using the method="post" from HTML itself. In my model class I have: public decimal Value { get; set; } – Sérgio Ribeiro Apr 17 '19 at 18:28
  • 3
    Could it be a culture issue? The comma is ignored as "just a grouping character" instead of decimal – Hans Kesting Apr 17 '19 at 18:34
  • Decimal is "." and not ",". Countries like Brazil use comma to decimal and not point. Use point instead.In C#: 150.00M(decimal) and not 150,00M. – pnet Apr 17 '19 at 19:15
  • For the decimal types you may have to define your own model binders, like -http://www.dotnetfunda.com/articles/show/3259/passing-decimals-to-action-methods-in-mvc – Vishnu Apr 17 '19 at 19:19

2 Answers2

2

Give the number input with dot "." not comma ","

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/decimal

For example, C# decimals are given like this, decimal myMoney = 300.5m;

K4R1
  • 745
  • 1
  • 10
  • 20
0

Problem solved using jQuery maskMoney plugin.