I am trying to get the Bootstrap Datepicker to work. I followed instructions from these sources:
How to add Date Picker Bootstrap 3 on MVC 5 project using the Razor engine? (Answer bei Enzero)
&
http://bootstrap-datepicker.readthedocs.org/en/latest/options.html#format
The problem is, that instead of the common american mm/dd/yyyy format, I want to use the german format (dd.mm.yyyy). So I did the following:
In Model:
[Required]
[DataType(DataType.Date)]
[Display(Name = "Datum")]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Day { get; set; }
In Web.Config:
<system.web>
<globalization culture="de-DE" uiCulture="de-DE" />
...
</system.web>
In Scripts\DatePickerReady.js:
if (!Modernizr.inputtypes.date) {
$(function () {
var date = new Date();
date.setDate(date.getDate());
$(".datecontrol").datepicker({
startDate: date,
format: 'dd.mm.yyyy'
});
});
}
Which is essentially what I want. The problem is now that validation says that the date format is wrong.
"The field Datum must be date"
So the question is, why is validation not accepting the date?
EDIT #1:
I also use an EditorTemplate for date: (Views/Shared/EditorTemplates/date.cshtml):
@model Nullable<DateTime>
@{
DateTime dt = DateTime.Now;
if (Model != null)
{
dt = (System.DateTime) Model;
}
@Html.TextBox("", String.Format("{0:d}", dt.ToShortDateString()), new { @class = "form-control datecontrol", type = "date" })
}
EDIT #2: I just read that DisplayFormat is not recognized when you use a TextBox (instead of e.g. an EditorFor). That explains why Validation is not working. But I still don't know how to solve it, yet.
EDIT #3:
This is the HTML code for the Date field that I get I see in IE F12:
<input name="Day" class="form-control datecontrol input-validation-error" id="Day" aria-invalid="true" aria-required="true" aria-describedby="Day-error" type="date" value="11.08.2015" data-val-required="The Datum field is required." data-val="true" data-val-date="The field Datum must be a date.">
The scripts that are loaded on the page are: jquery-2.1.4.js, bootstrap.js, bootstrap-datepicker.js, DatePickerReady.js, respond.js, jquery.validate.js, jquery.validate.unobtrusive.js
EDIT #4: I now use this in DatePickerReady.js:
if (!Modernizr.inputtypes.date) {
$(function () {
var date = new Date();
date.setDate(date.getDate());
$(".datecontrol").datepicker({
startDate: date,
format: 'dd.mm.yyyy',
autoclose: true,
todayHighlight: true,
weekStart: 1,
language: 'de-DE',
calendarWeeks: true
});
});
}
Every attribute works, but the 'de-DE' does not seem to do a thing. Language of the days is still in english. The validation issue also persists.
Further information: ASP.NET MVC 5, .NET 4.6, Bootstrap.Datepicker 1.4.0, jQuery 2.1.4, jQuery.Validation 1.14.0, Microsoft.jQuery.Unobtrusive.Validation 3.2.3