MVC internationalization/localization weirdness

I’ve just encountered a weird decision in the ASP.Net MVC framework that I don’t understand.  When it comes to interpreting parameters especially complex ones being done via ModelBinding it will assign the parameters different cultures depending on where the parameters came from.  I’m kind of wondering why that’s the case.

The specific reason I now know this, and have fudged my code to get around that is that I’m passing dates on the query string using a form with a GET.  Since I’m in little england my date got read the wrong way and I kept getting my month and day reversed. Previously the code I’d written that was attached to a form that did a POST worked fine, and continued to while I was pulling my hair out wondering why my new code was broken.

The part that appears to be responsible for this is the ValueProviderDictionary and there we find this comment.

// We use this order of precedence to populate the dictionary:
// 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

As I’ve implemented my own date model binder like Scott Hanselman I’ve put this fudge into it,

// KLUDGE: force the culture since for some reason the MVC platform only uses the
// users culture if the results come from a form, not if they are from a route or query string.
if (valueResult.Culture == CultureInfo.InvariantCulture)
    valueResult = new ValueProviderResult(valueResult.RawValue, valueResult.AttemptedValue,
return (T?)valueResult.ConvertTo(typeof(T));

It’s nuts but it works around my problem.  There may be a better way to hook into the framework but since I don’t really understand their reasoning for doing that I’ve decided to leave it alone for now.


3 thoughts on “MVC internationalization/localization weirdness

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s