1

i have set culture in Action Filer as

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

where culture = {fr-be} =>French Belgium.

FYI, this action filter sets the culture on user choice.

in one of myAction user is entering Date in format [dd/mm/yyyy] => 26/7/2011. Action is as follows

    public ActionResult RequestVacation(VacationRequest model)
    {
        if(ModelState.IsValid)
        {

....

when i dubug the code model.VacationDate contains 01/01/0001 ; although it should be 7/26/2011 whereas Form[VacationDate] contains 26/07/2011 [which is in Fr-BE formate] And ModelState.IsValid is false; although it should be true, as date is correct in fr-be format. when i furtur dig out but checking locals in visual studio i found

this.ModelState[1].Culture = {en-US}

whereas i had already set culture value using actionFilter, as stated above. My question is how can i set this.ModelState.Culture = {fr-be}?

  • "when i dubug the code model.VacationDate contains 01/01/1900 ; although it should be 7/26/2011" - January 1st, 1900 is minimal date in SQL Server. Although you didn't mention that you are using some kind of DB, it seems that the this problem is not related to CultureInfo. I would try to fix it first, then proceed to fixing other issues... – Paweł Dyda Jul 08 '11 at 21:26
  • u have guessed correct, db is SQL Server. plz consider this line too. "Form[VacationDate] contains 26/07/2011 [which is in Fr-BE formate] And ModelState.IsValid is false; although it should be true". so the problem here is date exist in requestSring whereas while validation, it is not being parsed. – abdul qadir memon Jul 09 '11 at 21:28

3 Answers3

5

A bit late to answer your question but I imagine that what is happening is you are setting the current thread culture in the OnActionExecuting method of an action filter, but model binding happens prior to that so it doesn't pick up your change.

You'll need to move your code that sets the culture to earlier on in the pipeline, before model binding occurs, for example in the Application BeginRequest method, or AcquireRequestState if you need to store the culture in session.

Matt B
  • 8,315
  • 2
  • 44
  • 65
  • 1
    You were exactly right. I was setting the culture on a base controller OnActionExecuting. Although that executed before the action executing, it was apparently executing only after the ModelState had been instantiated. Thanks – Fabio Milheiro Aug 03 '15 at 22:38
2

Put the following in your web.config:

<configuration>
   <system.web>
      <globalization
           fileEncoding="utf-8"
           requestEncoding="utf-8"
           responseEncoding="utf-8"
           culture="fr-BE"
           uiCulture="fr-BE"
        />
   </system.web>
</configuration>
Michal B.
  • 5,676
  • 6
  • 42
  • 70
1

In response to my above question i had solved it in this way

            if (ModelState.Keys.Contains("VactionDate"))
        {
            ModelState err = ModelState["VactionDate"];
            if (!err.Value.Culture.Equals(Thread.CurrentThread.CurrentCulture))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(err.Value.AttemptedValue, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
                    model.VactionDate = dt;
                    ModelState.Remove("VactionDate");
                }
                catch
                {
                }
            }
        }

i know this is not a good solution. but i m still looking for some way to change, before validation occurs,

ModelState[n].Value.Culture = {en-US}

To

ModelState[n].Value.Culture = {fr-BE}

where {fr-BE} is my required Culture, for dateTime to be parsed. so i m still looking for someone to find out a good solution for this.