When binding data for GET, InvariantCulture
is used(which is "en-US"), whereas for POST Thread.CurrentThread.CurrentCulture
is. The reason behind is that GET urls may be shared by users and hence should be invariant. Whereas POST is never shared and it is safe to use server's culture for binding there.
If you are sure your application does not need the option of sharing urls between people coming from different countries, you are safe to create your own ModelBinder
that will force to use server locale even for GET requests.
Here is the sample how it may look like in Global.asax.cs:
protected void Application_Start()
{
/*some code*/
ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder());
}
/// <summary>
/// Allows to pass date using get using current server's culture instead of invariant culture.
/// </summary>
public class DateTimeModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var date = valueProviderResult.AttemptedValue;
if (String.IsNullOrEmpty(date))
{
return null;
}
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
try
{
// Parse DateTimeusing current culture.
return DateTime.Parse(date);
}
catch (Exception)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid.", bindingContext.ModelName));
return null;
}
}
}