0

I have a search form with inputs for date from and date to and uses a Jquery DatePicker

I have tried every combination of the annotations below

I have a view model with the following

    [Display(Name = "Date From")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DateFrom { get; set; }

    [Display(Name = "Date To")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime DateTo { get; set; }

And a View with the following

        <div class="form-group">
            @Html.LabelFor(m => m.DateFrom)
            @Html.TextBoxFor(m => m.DateFrom, "{0:dd/MM/yyyy}", new { @class = "form-control" })
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.DateTo)
            @Html.TextBoxFor(m => m.DateTo, "{0:dd/MM/yyyy}", new {@class = "form-control"})
         </div>

And Script with the following (same for #DateTo)

    $("#DateFrom").datepicker({
        dateFormat: "dd/mm/yy",
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        maxDate: 0
    });

web.config has the below

<globalization culture="en-GB" uiCulture="en-GB" />

Ive looked at other issues on Stack Overflow but for me the date is still being interpreted at MM/dd/yyyy for both fields.

The correct format appears to come back to the controller correctly.

I get Client Side Validation Error The value '13/08/2015' is not valid for Date From. Anything 01-12/08/2015 will work

Frazer
  • 560
  • 2
  • 11
  • 21
  • `[DataType(DataType.Date)]` and `[DisplayFormat(ApplyFormatInEditMode = true)]` are only respected when using `EditorFor()` so they are unnecessary in your case. –  Sep 09 '15 at 09:39
  • Unclear what your asking. Are you saying that you get a client side validation error? –  Sep 09 '15 at 09:40
  • Yes I get Client Side Validation Error – Frazer Sep 09 '15 at 09:42
  • With Reference to the referenced Question (in duplicate) I have removed the Date Pickers completely for now and I still get the same issue. – Frazer Sep 09 '15 at 09:58
  • Not sure what you saying. Do you mean you have removed the scripts? –  Sep 09 '15 at 10:02
  • Yes, the Jquery.datepicker scripts have been removed (temporaily) – Frazer Sep 09 '15 at 10:03
  • Don't (the answer I gave showed how to handle this with the datapicker). But if you don't to use the datepicker anymore, then instead of `$.datepicker.parseDate('dd/mm/yy', value);` your going to have to replace it with you own validation code to override the default `jquery.validate` method (which validates it using `MM/dd/yyyy`). I do have code for this is you need it –  Sep 09 '15 at 10:07
  • I tried adding the $.validator.addMethod('date', function (value, element) {... code to above my datepicker declarations. The datepickers didnt work. (I have no problem using EditorFor BTW) – Frazer Sep 09 '15 at 10:56
  • You need to add the script once just after where you include `jquery.validate.unobtrusive.js` (and not inside `document.ready()`) - not inside the datapicker declarations –  Sep 09 '15 at 10:58
  • Im not currently using JQuery Validate or jquery.validate.unobtrusive, is this something i should add for this to work? – Frazer Sep 09 '15 at 11:02
  • Then how are you getting those client side validation messages? –  Sep 09 '15 at 11:03

0 Answers0