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

Return date format in DateTimePicker component

7 Answers 505 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Mario
Top achievements
Rank 2
Mario asked on 24 Sep 2018, 02:51 PM

Hi,

I have a problem with DateTimePicker component on Update action on GridView. 

In my model I've annotated the property with the following attributes:

        [Required]
        [DataType(DataType.Date)]
        [Display(Name = nameof(MessageResources.DateSent), ResourceType = typeof(MessageResources))]
        public DateTime DateSent { get; set; }

In the columns section of a gridview component custom formating is set up as following:

    .Columns(columns =>
    {
        columns.Command(command => { command.Edit(); }).Width(100);
        columns.Bound(x => x.DateSent)
        .Format("{0: dd.MM.yyyy HH:mm:ss}")
        .Title("Date Sent");

        columns.Command(command => { command.Destroy(); }).Width(100);
    })

And I have a custom pop-up form with the following  syntax and format:  

                @(Html.Kendo().DateTimePickerFor(p => p.DateSent)
                                            .Format("dd.MM.yyyy HH:mm")
                                            .HtmlAttributes(new { @readonly = "readonly", @class = "form-control" }))

On put or post operations default value (01/01/0001 00:00:00) is sent to the controller. In case I made model property nullable and without required then this value is always null.

 

7 Answers, 1 is accepted

Sort by
0
Alex Hajigeorgieva
Telerik team
answered on 27 Sep 2018, 02:48 PM
Hello, Mario,

Thank you for the provided code snippets.

Can you let me know why the editor has a read-only attribute?

Since the model date is read-only, it is expected that a required DateTime will be created on the server with the default value that you shared with us. Naturally, a nullable DateTime will not be generated by ASP.NET Core.

In case the DateTime already has a value, then I would guess that the globalization is at fault. Can you confirm that the client and the server side have the same culture?

This is the official globalization demo: https://demos.telerik.com/aspnet-core/grid/globalization

Look forward to hearing back from you.

Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mario
Top achievements
Rank 2
answered on 28 Sep 2018, 06:59 AM

Hi Alex,

It is mentioned for the editor to be user input and then I've run into this issue so I made the editor read-only and create the workaround for setting the date in the backend.

I've already tried the globalization from the demo that you've suggested.
I put in Startup.cs following code:

            IList<CultureInfo> supportedCultures = new List<CultureInfo>
            {
                new CultureInfo("en-GB")
            };
            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en-GB"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });

Add in _Layout.cshtml the js script for the culture:

<script src="~/lib/kendo-ui/js/cultures/kendo.culture.en-GB.js"></script>

And added all the sections realated to setting the culture on client side:

@{
    var culture = System.Globalization.CultureInfo.CurrentCulture.ToString();
}

@section Scripts{
    <script type="text/javascript">
    kendo.culture("@culture");
    </script>
}

The result of DateSent when I call the Put and Post operation is null and the date is set. I've added Debugging screenshots with pictures of the result that I got on action and also what I've got in the page source 

 


0
Mario
Top achievements
Rank 2
answered on 28 Sep 2018, 07:12 AM

Also, the selected value is actually send to the back-end but as you can see, this is the error that I've got on me method:

ModelState.Values.SelectMany(v => v.Errors).Select(x=>x.ErrorMessage)
{System.Linq.Enumerable.WhereSelectEnumerableIterator<Microsoft.AspNetCore.Mvc.ModelBinding.ModelError, string>}
    [0]: "The value '8/21/2018 3:54:29 PM' is not valid for Sent Date."

0
Mario
Top achievements
Rank 2
answered on 28 Sep 2018, 11:25 AM
I created a small demo project that reflects this issue. Since I cannot attach the zip with the asp.net core project to this thread. Here is the demo project: TelerikDatePickerDemo
0
Preslav
Telerik team
answered on 02 Oct 2018, 11:47 AM
Hi Mario,

Thank you for creating the demo project. Unfortunately, we do not have access to download it. Having said that, could you please send it via another file-sharing service? For example, Dropbox, or anything else that works for you.

I look forward to your reply.


Regards,
Preslav
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Mario
Top achievements
Rank 2
answered on 02 Oct 2018, 11:59 AM

Hi Preslav,

I've added the project to Dropbox: TelerikDatePickerDemo

 

0
Viktor Tachev
Telerik team
answered on 05 Oct 2018, 03:30 PM
Hello Mario,

I apologize for the late reply. 

When investigating the behavior with the sample project we noticed that the date was sent to the server in en-US format by default. This was done in order to fix an old issue with binding numeric and date values in different cultures. 

Currently as a workaround I can suggest using a Custom() data source and including parameterMap function. This way the Date field can be sent to the server in the expected format. 

I apologize for any inconvenience this can cause you.


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Date/Time Pickers
Asked by
Mario
Top achievements
Rank 2
Answers by
Alex Hajigeorgieva
Telerik team
Mario
Top achievements
Rank 2
Preslav
Telerik team
Viktor Tachev
Telerik team
Share this question
or