I have an MVC5 Web application and am trying to get the JQuery DatePicker to work with DateTime EditorTemplates.
For the editor template I have;
@model System.DateTime?
@if (Model.HasValue)
{
@Html.TextBox("", String.Format("{0:ddd MMM dd,yyyy}", Model.Value), new { @class = "datepicker" })
}
else
{
@Html.TextBox("", String.Format("{0:ddd MMM dd,yyyy}", DateTime.Now), new { @class = "datepicker" })
}
the Javascript;
<script>
$(document).ready(function () {
$(".datepicker").datepicker({
dateFormat: 'ddd MMM dd,yyyy',
autoClose: true,
todayBtn: true,
todayHighlight: true
});
});
</script>
and finally the code;
<div class="col-lg-2">
<label>Create Date</label><br/>
@Html.EditorFor(model => model.CreateDate, new { @value = @Model.CreateDate" })
</div>
When I click on the control the DatePicker is displayed but;
1) It doesn't autoclose as it should.
2) If I don't select a date and click off somewhere else the current date is inserted.
3) When it first comes up the date is in the format I specified which is; "ddd dd MMM,yyyy" then when the date is inserted it is "MM/dd/yyyy" forever after.
What am I missing?
Thanks y'all