views:

54

answers:

2

I posted a decimal formvalue in InvariantCulture (16.4) and was surprised that it didnt get to the Model on my german system.

I had to use the german format (16,4). Is there a way to configure the language that is used for parsing parameters?

EDIT: After debugging into the sourcecode found the language specific parts in ValueProviderDictionary

PopulateDictionary. The doumentation there reads:

  1. Request form submission (should be culture-aware)
  2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
  3. URI query string

1.takes CurrentCulture 2./3. take InvariantCulture

This doesnt work for me: I would not expect a different behaviour regarding use of Culture if I post a value or put it in the querystring. It might be necessary if the user fills out parsable values by hand, but its strange if you fill the values for a Listbox. I thing the ideal solution would be, if I could switch between both versions.

Anyway I needed to find a way now to set CultureInfo.CurrentCulture before PopulateDictionary gets called. Thats how I did it:

        void Application_BeginRequest(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

    }
+1  A: 

The default parsing uses the CurrentCulture. However, all the parsing methods take an IFormatProvider (which can be a CultureInfo or depending on the thing you are parsing, a NumberFormatInfo or DateTimeFormatInfo). You should use the InvariantCulture for this scenario (anything in the backend that is not displayed to the user should be using the InvariantCulture.)

Eric MSFT
A: 

This solved the problem. But beware: if the user enters values in their local format it will not be parsed. This will hardly happen in my application. Hopefully UI components I use will allow for different culture for displaying and posting values.

    void Application_BeginRequest(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

    }
Malcolm Frexner