0

I started to get a View error Field NextContactDate is not a Date when we changed from Internet explorer to Edge. I wanted to resolve this and stop it from happening. This was in a C# application originally written in 2012 and running on PCs where they had replaced WIN 7 PCS with WIN 10. The original View Code for the field in Edit.cshtml was:

<tr>
        <td>@Html.LabelFor(model => model.sitestatus.StatusDate)</td>
        <td>@Html.TextBoxFor(m => m.sitestatus.StatusDate, new { @Value = Model.sitestatus.StatusDate.ToString("dd/MM/yyyy"), id = "dp", style = "width:90px;" })
        @Html.ValidationMessageFor(model => model.sitestatus.StatusDate)</td>
        <td>The Date of the Status Change</td>
</tr>

How do we get this to work?

1 Answers1

0

This was resolved by amending the Site Status Edit.cshtml code:

<tr>
        <td>@Html.LabelFor(model => model.sitestatus.StatusDate)</td>
        <td>@{ Html.EnableClientValidation(false); }
        @Html.TextBoxFor(m => m.sitestatus.StatusDate, "{0:dd/MM/yyyy}", new { id="dp", style="width:90px;" })
        @Html.ValidationMessageFor(model => model.sitestatus.StatusDate)
        @{ Html.EnableClientValidation(true); }</td>
</tr>

The trick was to include Html.EnableClientValidation false around the code but this is a new one on me. Anyone know what it does and what else it may affect ie does it allow Chrome to work on dates the same as IE and Edge?

  • Disabling validation is not a fix, it's a very serious bug. The original code was buggy - instead of binding to a date property and letting the client format it, it converted the DateTime value into a localized string. – Panagiotis Kanavos Sep 22 '20 at 13:30
  • All modern browsers have [date and datetime input types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date). Perhaps a better solution would be to use `input type="date"`. [This probably duplicate question shows how to do this](https://stackoverflow.com/questions/25086909/how-to-use-html-textboxfor-with-input-type-date) – Panagiotis Kanavos Sep 22 '20 at 13:33
  • I am all for a better solution – Bernard Ashton Sep 22 '20 at 13:34
  • Thanks so much for your rapid reply. I am all for a better solution but this was the best I could come up with after spending ages trawling the internet. If you are able to provide a better one I will try it and report back. – Bernard Ashton Sep 22 '20 at 13:35
  • Check the duplicate in that case. There were a lot of changes since 2012 though, both in HTML and Javascript, and the 8-year old validation scripts may fail in other places. The error you posted came from Javascript, because the string value couldn't be parsed as a Date. You may have to upgrade the client-side validation libraries – Panagiotis Kanavos Sep 22 '20 at 13:36
  • A *lot* of validation problems go away simply by using the "new" input types. – Panagiotis Kanavos Sep 22 '20 at 13:37
  • Thanks Panagiotis this is the first time I have had such a confident reply - I have not tried the potential solution yet as it looks like it may require a db change which may take a while to implement. The fix that looked like it had worked was only a change to the View and nothing else which is what made it attractive. – Bernard Ashton Sep 22 '20 at 13:49