2011-11-14 11 views
2

北美的日期格式是MM/DD/YYYY.NET DataAnnotation和全球化問題(服務器端)

我工作的項目爲澳大利亞(asp.net MVC 2)其中,日期格式爲d/MM/YYYY

在web.config中我有

 <globalization 
     fileEncoding="utf-8" 
     requestEncoding="utf-8" 
     responseEncoding="utf-8" 
     culture="en-AU" 
     uiCulture="en-AU" 
     enableClientBasedCulture="true" 
    /> 

在視圖中.NET呈現在正確的格式的日期 - 「EN-AU」,但是當我提交表格14/11/2011約會我ModelState.IsValid等於False。

如何教dataannoation以「en-AU」格式正確驗證日期?

//更新

剛剛發現只得到

using(Html.BeginForm("Search", "form", FormMethod.Post)) //Works 
using(Html.BeginForm("Search", "form", FormMethod.Get)) //Does'n work 

**有關這個問題看起來是一個.NET錯誤!

我嘗試了新的MVC2/3項目

當我使用GET,MVC結合不使用當前區域性**

謝謝。

回答

1

最後我有解決方案謝謝你幾十個不同的博客文章和文章。完美的工作,加上你可以應用它不只是日期。

//Global.asax.cs 
protected void Application_Start() 
{ 
    ModelBinders.Binders[typeof (DateTime)] = new ForceCultureModelBinder<DateTime>(); 
    ModelBinders.Binders[typeof(DateTime?)] = new ForceCultureModelBinder<DateTime>(); 
} 

和類

public class ForceCultureModelBinder<T> : IModelBinder where T : struct 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     if (bindingContext == null) 
      throw new ArgumentNullException("bindingContext"); 

     T? valueAttempt = GetA(bindingContext); 
     return valueAttempt == null ? (object) null : valueAttempt.Value; 
    } 

    private T? GetA(ModelBindingContext bindingContext) 
    { 
     ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 

     if (valueResult == null) 
      return null; 

     T? result; 
     try 
     { 
      result = (T?) valueResult.ConvertTo(typeof (T), Thread.CurrentThread.CurrentCulture); 
     } 
     catch (Exception) 
     { 
      bindingContext.ModelState.AddModelError(
        bindingContext.ModelName, 
        ex.InnerException.Message); 
      return null; 
     } 

     return result; 
    } 
} 
+0

您的示例對於使用用戶區域設置轉換參數輸入值非常有用,我在其中一個應用程序中使用了它,我只在代碼中添加了一行代碼爲了有一個錯誤信息的catch。關心 –

0

Hanselman在如何處理這個客戶端,在那裏,他確實像一個很好的職位:

$(document).ready(function() { 
     //Ask ASP.NET what culture we prefer 
     $.getJSON('/locale/currentculture', function (data) { 
      //Tell jQuery to figure it out also on the client side. 
      $.global.preferCulture(data); 
     }); 
    }); 

來源:http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

我相信,該插件現在稱爲Globalize的(發現here),但API應該類似,如果不完全相同。

+0

我的問題是在服務器端。代碼在視圖層面上正確渲染所有東西(我現在不觸摸瀏覽器端),但在服務器端提交表單(提交相同值)後,數據註釋引擎期望數據在en-US – Cherven

6

此行爲是設計使然。當您執行GET請求時,會將日期放入URL中。 ASP.NET MVC假設URL中的日期是不變的文化。

爲什麼?

嗯,假設我創建了以下URL:/ posts/01-11-2011 /併發送給你。這對你來說代表着什麼?

那麼這意味着顯示我2011年1月11日的帖子,因爲這對我的文化有效。但是您的文化可能將其表示爲2011年11月1日。網址應該唯一標識資源。取決於誰在看它,相同的URL不應具有不同的含義。

因此,我們決定我們的模型活頁夾不會將URL中的值轉換爲當前的文化,但轉換爲表單帖子中的值。這對我們來說是有意義的,因爲表單帖子不能發送,通常表示用戶的輸入。

+0

謝謝,它解釋了情況。我同意你,如果它是路徑的一部分,但在我的情況下url是/posts?...&date=01-11-2011 獲得參數我認爲文化必須工作。而在萬一的情況下,它必須是GET。 我已經花了2天的時間來找到如何教mvc正確解析數據。目前還沒有結果:( 能給我看一些示例嗎? – Cherven