0

I am devloping an MVC 5 Website and running into a cirtical Problem.

In my Database i have a Decimal(8,6) and i just wanna have it to be on my view and be validatey in my form

I am using this helper:

@Html.TextBoxFor(x=>x[i].DecimalValue, new{ type="number"})

The Result is, that my Value is not shown because the input field can't handle values like "15,11" only values like "15.11"

I even tried setting the value manually and replacing the comma with dot but then i cant send my decimal back to my controller it's alwasy 0 then.

I guess it's just a simple thing but i just cant get it to work properly.

Any Help on this?

Markus_DE_HH
  • 1,061
  • 3
  • 13
  • 29
  • not sure that I understand you... Decimal(8,6) means it saves 8 digits before the decimal and 6 after... of-course you can't save 15,11 in it.. that's not a valid number for that... – LiranBo Oct 26 '15 at 17:21
  • 1
    Take a look at http://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app – Jacob Oct 26 '15 at 17:40

2 Answers2

1

I presume you need non-English user data entry (German perhaps based on your name/handle) for your decimals.

Problem is, the way decimals are stored in .NET and in databases don't like that format.

You could treat it as string input however and apply your own regex to your DecimalValue property in your ViewModel using annotation attributes, like so:

[RegularExpression("(\\d{1,8}\\,\\d{1,6})", ErrorMessage = "Enter a valid decimal number")]

Before you then shove it into your database, you will need to convert the string to a proper decimal using the CultureInfo class, for instance:

decimal amount = decimal.Parse("15,11", CultureInfo.GetCultureInfo("de-DE"));
Wim
  • 11,998
  • 1
  • 34
  • 57
1

A better approach is to create your own model binder that handles decimal values. Instead of duplicating the logic, please refer to this question on SO

Community
  • 1
  • 1
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155