I'm working with ASP.NET MVC 4 and I'm getting a DateTime format issue.
In my view, I have 2 hidden elements which contains DateTime variables (from my ViewModel), a start date and an end date.
When I'm debugging, I can clearly see that my date format is correct (for instance: 17/06/2014 8:30:00). Then, when I'm trying to get these values to pass them to my post action, I got some weird DateTime formats: the day becomes month and the month becomes the day (still in my example, 17/06/2014 8:30:00 becomes 06/17/2014 8:30:00).
When I inspect the HTML code, I can clearly see that the change is done while the HTML code is generated:
<input data-val="true" data-val-required="Le champ Date de début : is required." id="StartDate" name="StartDate" type="hidden" value="06/17/2014 08:30:00" />
So, obviously, my ModelState is invalid in my controller and I can do nothing. Here's my View :
@using (Html.BeginForm("ConfirmAppointment", "Home", FormMethod.Post, new { avm = Model}))
{
@Html.HiddenFor(a => a.StartDate)
@Html.HiddenFor(a => a.EndDate)
//some code
}
Any idea about how to get the correct DateTime in order to pass it to my controller?
EDIT : My ViewModel :
public class AppointmentViewModel
{
[DataType(DataType.DateTime)]
public DateTime StartDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime EndDate { get; set; }
public string DisplayDate { get; set; }
}
DefaultModelBinderand the date format for the DateTime variables will be decided on your CurrentUICulture of the application. Which is by default will be equivalent to your System Culture