2010-05-28 135 views

回答

1

ASP.NET MVC預計DateTime值的格式爲Thread.CurrentLanguage。請檢查您正在使用的語言。

就是這樣,因爲用戶可以在文本框中輸入日期,並且他們會輸入他們的語言格式。

像Pieter說的:一個簡單的方法是在這種情況下使用一個字符串。

另一種方式是在Global.asax中使用

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; 
} 

您可以ModelBinding happend後切換回用戶的語言過濾器:

public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
      string language = //find a way to get the language - I have it in the route 
      Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language); 
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language); 
     base.OnActionExecuting(filterContext) 
    } 

這種方式有一定的風險,但我發現在很多情況下,它更容易,如果modelBinding使用InvariantCulture的(想想路線中的十進制值,路線中的日期時間......)

0

我認爲將日期的字符串表示形式傳遞給ASP.NET MVC動作並嘗試使用DateTime.TryParse(..)解析它是最簡單的。

[HttpPost] 
public ActionResult(string dateTimeString) 
{ 
    DateTime tempDateTime; 
    if(DateTime.TryParse(dateTimeString, out tempDateTime)) 
    { 
     //Handle 
    } 
} 
+0

實際上,日期時間是我模型的一個字段,我使用的是默認的modelbinder。有沒有辦法以正確的格式從客戶端傳遞日期? – Elangovan 2010-05-28 07:32:40

+0

哦,檢查Malcolm Frexner的迴應:) – 2010-05-28 08:11:16

相關問題