I have following field in my ViewModel:
[Display(Name = "Datum und Uhrzeit der Vorstellung")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy HH:mm}")]
public DateTime DateAndTimeOfPerformance { get; set; }
And display it the following way in a partial view:
<div class="form-group">
@Html.LabelFor(model => model.DateAndTimeOfPerformance, new { @class = "control-label" })
<div class="input-group date datetimepicker">
@Html.TextBoxFor(model => model.DateAndTimeOfPerformance, "{0:dd.MM.yyyy HH:mm}", new { @class = "form-control", @Value = Model?.DateAndTimeOfPerformance.ToString("dd.MM.yyyy HH:mm") })
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
@Html.ValidationMessageFor(model => model.DateAndTimeOfPerformance, null, new { @class = "text-danger" })
</div>
Like you can see, I use Bootstrap's datetimepicker:
$('.datetimepicker').datetimepicker({
format: "DD.MM.YYYY hh:mm"
});
But I always get the validation error:
The field Datum und Uhrzeit der Vorstellung must be a date.
From my research on Stackoverflow I saw that it can be tricky with a DateTime but I didn't find an example that matches mine to see how to make this work.
Is there any way to combine MVC validation and Bootstrap's Datetimepicker?
Thanks