2011-04-14 67 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类中。