2012-02-07 78 views
0

我有一個ASP.NET MVC網站上的表單,我最近從POST更改爲GET。但是,當我這樣做時,我已經意識到相關操作中的DateTime參數現在有效地使用不同的文化 - en-US用於GET,而不是en-GB用於POST。所以基本上這個日子和月份都在變換。DateTime本地化的自定義ModelBinder

以下文章向我解釋了爲什麼這樣做,但是我需要找到一種方法來克服它,而無需恢復到POST或使用JS在提交之前修改表單。

http://xhalent.wordpress.com/2011/05/14/localization-of-dates-in-asp-net-mvc/ http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

我創建了一個自定義的粘合劑日期時間,只是爲了試驗出一種理論,但這樣做似乎已經解決了這一問題。任何人都可以解釋爲什麼這有效嗎

定製粘合劑基本上在global.ascx.cs

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder()); 

下面一行,這爲DateTimeModelBinder類

public class DateTimeModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     return bindingContext.Model; 
    } 
} 

回答

0

看來JS是做到這一點的唯一方法。我認爲上面的答案只是返回模型構造函數設置的值。

相關問題