1

we use the JQuery UI Datepicker.

When i enter the date in this format: 9/22/2012 asp.net MVC says it is not in a valid format.

When i look at the viewmodel which is filled via the binding, it says for the datefield: NULL.

When i look at the request, i see this as the posted value: 9%2f22%2f2012

in the protected override void OnActionExecuting(ActionExecutingContext filterContext)) I've set Culture like this

            CultureInfo culture = new CultureInfo(language.LanguageISO);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture; 

where language.LanguageISO is, in this case, 'en'

I thought that the model binder would pick up the culture?

Can it be that it is a problem that the slash in the date is escaped as %2f?

3 Answers 3

3

It's too late to set the culture in the OnActionExecuting event. The model binder has already ran at this stage. You need to set it earlier, before the model binder runs. For example you could implement the IAuthorizationFilter interface:

public class SomeCustomAttribute : ActionFilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        // TODO: set the culture here so that it's picked up by the model binder

        CultureInfo culture = new CultureInfo(language.LanguageISO);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture; 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Darin, you scare me, you know just too much, this indeed did the trick!
0

try to configure datepicker like

$('.has_datepicker').datepicker({

       dateFormat: '@System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("M", "m").Replace("yy", "y")'

Comments

0
Follow the step:
1. @Html.TextBox("date", Model.ToString("dd/MM/yyyy"), new { @class = "date" })

2.
/// <reference path="jquery-1.4.4.js" />
/// <reference path="jquery-ui.js" />
$(document).ready(function () {
    $('.date').datepicker({dateFormat: "dd/mm/yy"});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.