1

The field DateTime must be a date.

I am using Kendo grid in MVC project. I have both French and English version of the same application. The following is what I do in the _Layout.cshtml file:

<script>
    if ("@Session["Culture"]" === "en-CA") {
        kendo.culture("en-US");
        Globalize.culture('en-CA');
    } else {
        kendo.culture("fr-CA");
        Globalize.culture('fr-CA');
    }

    $.validator.methods.number = function (value, element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    }

    $.validator.methods.date = function (value, element) {          
        if (this.optional(element))
            return true;

        var result = Globalize.parseDate(value, "@LCL.Common_Formats.DatePickerFormat", "@Session["Culture"]");

        return !isNaN(result) && (result != null);
    }


    $.validator.methods.min = function (value, element, param) {
        return this.optional(element) || Globalize.parseFloat(value) >= param;
    }

    $.validator.methods.max = function (value, element, param) {
        return this.optional(element) || Globalize.parseFloat(value) <= param;
    }

    $.validator.methods.range = function (value, element, param) {
        if (this.optional(element))
            return true;
        var result = Globalize.parseFloat(value);
        return (result >= param[0] && result <= param[1]);
    }
</script>

In the grid I have a datepicker as below:

 c.Bound(u => u.DateTime)
                    .Title(LCL.Common_Labels.PurchaseDate)
                    .Format("{0:" + LCL.Common_Formats.DatePickerFormat + "}")                    
                    .Width(smlColumn);

LCL.Common_Formats.DatePickerFormat is an entry in the Resources file for storing the datepicker formats. I have both English and French Resource files. My problem is that I am getting this weird exception: The field DateTime must be a date when editing the record in the French version.

enter image description here

But it works very well in the English version...

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
sony
  • 1,453
  • 3
  • 35
  • 79

2 Answers2

0

Those date formats ...

I had similar problem with formats. I can suggest you:

Get an instance of kendo validator with a configuration:

var kendoValudator = $('#yourFormId').kendoValidator({
    rules: {
        mvcdate: function (e) {
            if (e.is('[data-role="datepicker"]')) {
                var result = kendo.parseDate($(e).val(), 'dd/MM/yyyy');
                return !!result;
            }

            return true;
     }
}).data('kendoValidator');

Also if you are using jQuery on the page try adding:

jQuery.extend(jQuery.validator.methods, {
    date: function (value, element) {
        if ($(element).is('[data-role="datepicker"]')) {
            var res = this.optional(element) || kendo.parseDate(value, "dd/MM/yyyy") != null;
            return !!res;
        }

        return true;
    }
});

Then if your server receive a the default date (01/01/0001) you should change the server culture.

Example:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

I hope it helps.

mihkov
  • 1,171
  • 13
  • 37
0

I found the best solution for this:

var culture = kendo.culture();
if (culture.name === "fr-CA")
{
    culture.calendar.patterns.F = "dd/MM/yyyy";
}
else
{
    culture.calendar.patterns.F = "MM/dd/yyyy";
}

This has to be added at the document.ready() section.

I spend 3 days to find the solution for this...

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
sony
  • 1,453
  • 3
  • 35
  • 79