This is a migrated thread and some comments may be shown as answers.

Date picker format does not persist in Edit Popup - MVC

3 Answers 849 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nirav
Top achievements
Rank 1
Nirav asked on 02 Apr 2013, 11:01 AM
Hi,

I have nullable datetime (in UI and as date in DB) field for birth date. But when I click on Edit action in grid, It still displays as yyyy/MM/dd instead of yyyy-MM-dd. Hence I am getting BirthDay is not valid date error message. I have not written any explicit validation.

In my cshtml page I have formatted it as:
columns.Bound(r => r.BirthDay).Width(100).Format("{0:yyyy-MM-dd}");

In my model I have initialized as:
[Display(Name = "Birth Day")]
[DataType(DataType.Date)]        
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? BirthDay { get; set; }

I can however generalized and apply formatting for my entire site by below code in global.asax but I want to apply it for specific pages only.
protected void Application_BeginRequest()
{
            CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
            info.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
            System.Threading.Thread.CurrentThread.CurrentCulture = info;
}

Please help!

Thanks,
Nirav

3 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 04 Apr 2013, 07:28 AM
Hi Nirav,

Basically the server culture have different format from the one set manually to the column resulting in different format for the validation and the DateTimePicker. I would suggest to check the "Grid Globalization" demo in the offline demos (which comes with your KendoUI for ASP.NET MVC installation) which demonstrate how to change dynamically the culture on the client and the server side and keep the same date format.

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Nirav
Top achievements
Rank 1
answered on 08 Apr 2013, 08:14 AM
Hi Vladimir,

Thanks for reply. I saw the offline demo of grid localization and it explains how to set and change the culture for specific page or grid.

I have in my example set the date format as "dd-MMM-yyyy" in view as well as in model explicitely.
Though while entering the data in add/edit grid popup it keeps giving me "Date is not valid format".
Now if I want to resolve this error I have to set the date format in CultureInfo as "yyyy-MM-dd".

Here my question is though I have set the date format I want to display and get as an input in view and model, why I have to again set it to in CultureInfo and as "yyyy-MM-dd"?

Is it because I have date format as "yyyy-MM-dd" on my local machine? What if when I deploy my site on server and server has any other format?

Regards,
Nirav
0
Vladimir Iliev
Telerik team
answered on 10 Apr 2013, 07:01 AM
Hi Nirav,  

Basically the Grid validates the current edited fields using Kendo Validator by current field data type and current culture, therefore the validation will still fail because the current format of the DatePicker is "yyyy-MM-dd" which is different from the default format of your culture - "yyyy/MM/dd". To enable this custom format to pass the validator you can use on of the following two options:

  • edit the "kendo.culture.your_culture.min.js" script to change the default date pattern:     
    //only required part of the file is showed
      
    //default date pattern:
    patterns:{d:"dd/MM/yyyy"
      
    //should be changed to your custom pattern
    patterns:{d:"dd-MM-yyyy"
  • extend the Grid build-in validator:  
    (function ($, kendo) {
        //Extending the Grid build in validator
        $.extend(true, kendo.ui.validator, {
            rules: {
                // custom rules
                date: function (input, params) {
                    if (input.attr("data-role") == "datepicker") {
                //Specify your custom pattern
                        if (kendo.parseDate(input.val(), "dd-mm-yyyy")) {
                            return true;
                        }
                        return false;
                    }
                    return true;
                }
            }
        });
    })(jQuery, kendo);

Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Nirav
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Nirav
Top achievements
Rank 1
Share this question
or