2

When my form with the view below is being posted to my controller action MyAction, I get a 1/1/0001 12:00:00 AM date on my StartDate property, the format of the date coming in should be dd/MM/yyyy. I set it to 14/04/2011 on the form but it comes in 1/1/0001. Is there some culture or setting I'm not configuring properly?

In my controller, culture is set to

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

Action posted to:

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

View Model:

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

On View:

<%=Html.EditorFor(m => m.StartDate, new { style = "width: 110px;", @class = "date" })%>
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
  • Why do u use `EditorFor`? What does it output? Textbox? – Aliostad Apr 14 '11 at 19:34
  • @Aliostad, for reasons laid out here [MVC3 Html.TextboxFor vs. Html.EditorFor](http://stackoverflow.com/questions/4826173/mvc3-html-textboxfor-vs-html-editorfor). Yes, it outputs a textbox. – Luke Hutton Apr 14 '11 at 20:22

1 Answers1

0

My bad, I wasn't setting Thread.CurrentThread.CurrentCulture in MyAction, only in the Index action. I moved the logic into a BaseController class that all the controllers extend.

Luke Hutton
  • 10,612
  • 6
  • 33
  • 58