6

I have an MVC4 app which is compiled in the UK and hosted on a US server. In my model I have a date field which is required.

Attached to this field is a date picker, on selecting a date it will set a date to be UK format "DD/MM/YYYY" e.g. 23/12/2013. On submitting the form the application throws a validation error as it does not expect the the format.

In MVC4 how do I:

  1. Accept both date formats i.e. Override the date format? I have set the date format but cannot specify multiple date formats
  2. Server side I am saving the date to a database using Linq. This also throws an error as an incorrect format. I suspect I will need to convert to a UK date?
  3. When loading the details put the date in the correct format depending on the user's location
  4. Detect the location of the user server side?

Has anyone got any details on how to very validation based on globalisation of the user?

Thanks

CR41G14
  • 5,464
  • 5
  • 43
  • 64

2 Answers2

2

Did you add the below segment in web.config ?

<globalization 
    enableClientBasedCulture="true" 
    uiCulture="auto" 
    culture="auto" />

and read your date in code as DateTime format. that should not give you a problem.

Another way is to in you server side code read tha date always as DateTimeUTC and when ever you display the date on the screen, use .ToLocalTime()

HaBo
  • 13,999
  • 36
  • 114
  • 206
1

In the web config, in the globalization section, set the culture to "Auto". this will use user's current culture (including number and Date time format)

<globalization culture="Auto" />

The .net framework will parse and render data according to the user culture and both MVC and Entity will work properly.

Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41