0

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

Palmi
  • 2,381
  • 5
  • 28
  • 65
  • What is the value of your app's culture and uiCulture? You can set them in web.config. If not set, default will use en-EN and therefore your value cannot be parsed to a dateTime. – Jack B Apr 29 '16 at 17:44
  • By default, the` jQuery.validator` validates dates based on `MM/dd/yyyy` format so you need to reconfigure the validator. Refer [this answer](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969) for some options –  Apr 29 '16 at 22:07

1 Answers1

0

This is definitely an issue with the format of the value in your DateAndTimeOfPerformance variable. The control is not able to parse it into correct date time. Check the format.

Also, check the culture. It may be causing issues and jumbling DateTime into a format which datetimepicker is not recognising.

Use the control with datetimepicker and check the value being used. You can then go from there.

Aman Sharma
  • 1,930
  • 1
  • 17
  • 31