2

I have a property

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mmtt}")]
    public DateTime DeadlineTime { get; set; }

use it on page

    <div class="form-group">
        @Html.LabelFor(model => model.DeadlineTime, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DeadlineTime, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DeadlineTime, "", new { @class = "text-danger" })
        </div>
    </div>

but when I try to enter the value like : 11:30PM I get a validator error:

The field DeadlineTime must be a date.

why and how to solve it?

Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 1
    Why dont use `TimeSpan`? – Gene R Jan 31 '16 at 13:04
  • 1
    research http://stackoverflow.com/questions/16284385/the-field-must-be-a-date-datepicker-validation-fails-in-chrome-mvc – Cee McSharpface Jan 31 '16 at 13:06
  • @dlatikay I have problem with time, not date. So, datepicker does not link with my problem – Oleg Sh Jan 31 '16 at 14:35
  • I understand. still, the fourth answer in that article I mentioned hints at a problem which might apply in your situation: does the input control render as `` on client side? – Cee McSharpface Jan 31 '16 at 14:49
  • @dlatikay Of course, type will be "date" if type of model property is "DateTime". If I changed it on TimeSpan then attribute [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mmtt}")] would not work – Oleg Sh Jan 31 '16 at 15:04
  • [this answer](http://stackoverflow.com/questions/15986425/time-input-field-in-asp-net-mvc) suggests that you need to decorate the DeadlineTime declaration with an additional attribute: `[DataType(DataType.Time)]` – Cee McSharpface Jan 31 '16 at 18:06
  • 1
    `11:30PM` cannot be parsed to typeof `DateTime` (it has no year, month or day component. Your property needs to be `TimeSpan` or you need a custom `ModelBinder` to convert the value to a `DateTime` –  Jan 31 '16 at 22:53

0 Answers0