2011-04-14 53 views
2

當我的表單與下面的視圖張貼到我的控制器行動MyAction,我得到一個1/1/0001 12:00:00 AM日期在我的StartDate屬性,格式進入的日期應該是dd/MM/yyyy。我將它設置爲14/04/2011的形式,但它在1/1/0001。有沒有一些文化或設置我沒有正確配置?DateTime屬性沒有設置正確的查看模型

在我的控制,區域性設置爲

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-AU"); 

行動發佈到:

[HttpPost] 
public ActionResult MyAction(MyViewModel myViewModel) 
{    
    if (!ModelState.IsValid) return View("Index", myViewModel); 
    ... 

視圖模型:

public class MyViewModel 
{ 
    [DataType(DataType.Date)] 
    [DisplayName("Start Date")] 
    [Required] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] 
    public DateTime StartDate { get; set; } 
} 

上查看:

<%=Html.EditorFor(m => m.StartDate, new { style = "width: 110px;", @class = "date" })%> 
+0

爲什麼你使用'EditorFor'?它輸出什麼?文本框? – Aliostad 2011-04-14 19:34:59

+0

@Aliostad,由於這裏列出的原因[MVC3 Html.TextboxFor與Html.EditorFor](http://stackoverflow.com/questions/4826173/mvc3-html-textboxfor-vs-html-editorfor)。是的,它輸出一個文本框。 – 2011-04-14 20:22:35

回答

0

我的壞,我沒有設置Thread.CurrentThread.CurrentCultureMyAction,只在Index行動。我將邏輯轉移到所有控制器擴展的BaseController類中。