开发者

MVC3 globalization not set for ModelState.Value.Culture

开发者 https://www.devze.com 2023-03-19 03:41 出处:网络
i have set culture in Action Filer as Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

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 que开发者_StackOverflow中文版stion is how can i set this.ModelState.Culture = {fr-be}?


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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号