MVC4 Date Picker with Default Date

asp.net-mvc, asp.net-mvc-4, datepicker, razor

Solution

My problem was not a jQuery or MVC4 problem as I had initially thought. It has to do with how the HTML5 compatible browser displays the date picker. I was passing the date to the view in the incorrect format.

I modified my ViewModel to this and now the date populates correctly:

public class EmployeeIndexViewModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ToDate { get; set; }
}

Problem

I am using MVC4 with the Razor view engine. My controller is as follows: ``` public ActionResult Index() { return View(new EmployeeIndexViewModel { ToDate = DateTime.Now }); } ``` My View Model is as follows: ``` public class EmployeeIndexViewModel { [DataType(DataType.Date)] public DateTime ToDate { get; set; } } ``` On my view I have a simple `EditorFor()` call: ``` @Html.EditorFor(model => model.ToDate) ``` But still get a default (masked) value in my date picker: QUESTION: How can I get today's date (from controller) to display here instead?

Original source