asp.net mvc 3 - How to set the culture of all ValueProviderResult used in model binding to a given value? -
it turns out mvc's defaultmodelbinder
uses different cultures in order parse values (e.g. double
, datetime
, etc.) post vs requests. here more info.
i see controlled culture
property of valueproviderresult
objects, returned ivalueprovider.getvalue()
.
my question is: how can globally make sure value cultureinfo.invariantculture.
i know can implement custom value providers , way.
i know can implement custom model binders , way.
i know can set culture in thread, unfortunately not option in case.
what looking way set default model binder , existing value providers able parse in culture invariant way, irrespective of thread culture set to.
as far know, there no way match criteria. will have 1 of things know can (i'd proper way custom value provider).
reason: default valueproviders hardcoded use either cultureinfo.invariantculture
or cultureinfo.currentculture
.
here, specifically, way formvalueprovider
it:
internal formvalueprovider( controllercontext controllercontext, iunvalidatedrequestvalues unvalidatedvalues ) : base( controllercontext.httpcontext.request.form, unvalidatedvalues.form, cultureinfo.currentculture // <--- grrr, argh ) { }
the culture isn't retrieved anywhere else (i.e., argument above isn't used default, 1 culture use).
cultures of different ivalueproviders
for reference, these cultures each of default ivalueproviders:
- childactionvalueprovider: invariantculture
- formvalueprovider: currentculture
- jsonvalueprovider: currentculture
- routedatavalueprovider: invariantculture
- querystringvalueprovider: invariantculture
- httpfilecollectionvalueprovider: invariantculture
replacing currentculture
ivalueproviders
it isn't huge task replace formvalueprovider
, since, seen above, calls base class' (namevaluecollectionvalueprovider
) constructor - takes desired culture argument.
the original implementation of formvalueprovider appears on surface harder is, references internal classes , interfaces. they're not needed in order replace provider - they're there unit testing.
you need call base constructor (as mentioned above), passing 2 namevaluecollection
s easy acquire: request.forms
, forms
property of validation.unvalidated(request)
(a static method). , set third argument culture want.
the formvalueproviderfactory
more straightforward.
jsonvalueprovider
bit more involved - you'd have copy source of jsonvalueproviderfactory
new class , modify - because although allows overriding getvalueprovider()
, method consists of calls other private static
methods.
edit (petar ivanov): worked me. in order make work not enough add custom factory valueproviderfactories.factories
, because way it's added after formvalueproviderfactory
. instead, had replace formvalueproviderfactory
custom one.
Comments
Post a Comment