2

I am having a problem with dataformatting in SQL, C#, Razor and HTML5

In my database is a field which has to represent a currency, however the datatype is decimal(18,2), every value saved to the database is saved with an decimal point.

In my DTO the type is also decimal.

However when i give the EditorFor the type number, the field is null when it loads the page. When i give the EditorFor the type money, the field is filled with the correct value, however it places an comma instead of an decimal point which let it kick in an validation error when the form submits. I have to manually change the comma to an decimal point for it to correctly submit.

I have allready tried setting the type of the EditorFor to number and setting the step property but then it still keeps my input empty.

record in database
This is the record in the database



record on page
This is the record in my view



html markup
this is the generated html markup

UPDATE: I changed the globalization settings to en-US, this fixed the comma to decimal point question but screws up my date annotation, so down the line i'd like the data annotation to be nl-NL (or similar) and the number format to be en-US (or similar)



Note: it is not allowed to make any changes to the database regarding data-types.
Note: debugging is done on Google Chrome and Microsoft Edge

Maarten
  • 660
  • 1
  • 6
  • 21

3 Answers3

1

If in your model or ViewModel you can use this with data annotations:

[DisplayFormat(DataFormatString = "{0:$###,###}")]

If in your controller you can use:

string.Format("{0:$###,###}", yourContext.yourDataFieldname);

Ben-Carpenter
  • 189
  • 1
  • 6
0

Things like formatting and parsing are culture-sensitive.

For the server i.e. ASP.NET, that is the culture of the current thread/execution context.

For the client/browser, it sets the Accept-Language header depending on the browser configuration and/or the OS/regional settings. You could allow people to click a flag to change language by storing an override somewhere (session state, cookies, query string ?ln=en-us, route `http://localhost/nl-nl/home/index, ...)

It's probably best to honour Accept-Language first, then if needed allow the culture that would be implied be overridden by user action through one of the other methods.

Essentially, on the server, you need to set the culture and/or UI culture of the current execution context (which not necessarily is as coarse as the thread, since there is async/await and multiple requests might serve on the same thread, but that will be taken care of through the concept of the execution context automatically for you)

See

The last thing that might be needed or not - I don't quite know - is perhaps to add something to the validation attributes on the page if needed. But you might get lucky and find out that MVC does this automatically.

Note that you can/have to change the Windows Regional Settings to have IE/Edge send a different language list. Note that the list might actually contain a list of languages and you might need to match those with the languages of resources in your app (if applicable), perhaps picking another than the first one mentioned if say the client asks for Chinese, Dutch and US English (in that order), if in this case you want to serve using Dutch locale rather than culture nutral / invariant culture / by convention US English.

Community
  • 1
  • 1
Eric
  • 2,797
  • 2
  • 20
  • 19
-2

Try to put this in your asp.net web.config file:

<globalization
    culture="en-US"
    uiCulture="en-US"
/> 
Maarten
  • 660
  • 1
  • 6
  • 21
Wouter den Ouden
  • 1,523
  • 2
  • 17
  • 44
  • Thanks! However this did fix my comma to decimal problem, it screws up my date annotations. – Maarten Mar 04 '16 at 11:03
  • 1
    How is this a valid answer? Now you have to do other hacking to correct the currency symbol and correct the dateformat – pjdupreez Feb 13 '17 at 16:24