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

Binding to nullable DateTime

9 Answers 3481 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 10 Jun 2019, 04:33 PM

Hi,

Is there any way of binding to a nullable DateTime or is there a recommended approach for handling null/empty values. I think this question could apply to any of the controls that bind to a struct or an in-built type like Int/Decimal.

Thanks,

Nick.

 

Neil N
Top achievements
Rank 1
Iron
Veteran
Iron
commented on 30 May 2024, 04:57 PM

Is there a reason why nullable DatePickers return null when an invalid date is entered?

We have:

public DateTime MinDate { get; set; } = DateTime.Now.AddDays(30);

public DateTime? AutopayDuesEndDate { get; set; }

 

    <p><TelerikDatePicker Width="200px" @bind-Value="@AutopayDuesEndDate" Min="@MinDate"></TelerikDatePicker></p>
    <label hidden="@ValidEnrollDate">Please enter a date on or after @MinDate.ToString("MMMM d yyyy").</label>

            if (AutopayDuesEndDate == null || (AutopayDuesEndDate != null && AutopayDuesEndDate >= MinDate))
            {
                ValidEnrollDate = true;
            }
            else
            {
                ValidEnrollDate = false;
                return;
            }

If the user uses the picker to choose a date then an invalid date can't be chosen so that's ok. But they can manually enter an invalid date by typing it in and the if check doesn't work because AutopayDuesEndDate is set to null.

I know I can use an EditForm and a model with data annotation but that seems like overkill for one field. Is that the only way to get the validation we need (i.e., no date or date at least 30 days in future)?

 

9 Answers, 1 is accepted

Sort by
0
Nick
Top achievements
Rank 1
answered on 11 Jun 2019, 10:20 AM
I'm not sure if it helps, but there's a discussion in the Asp.Net core issues page about binding to nullable types: https://github.com/aspnet/AspNetCore/issues/5541
-1
Marin Bratanov
Telerik team
answered on 11 Jun 2019, 11:52 AM
Hi Nick,

Nullable DateTime is still not supported and you can track this feature in the following page: https://feedback.telerik.com/blazor/1410422-binding-to-nullable-value.


Regards,
Marin Bratanov
Progress Telerik UI for Blazor
0
Nick
Top achievements
Rank 1
answered on 11 Jun 2019, 12:28 PM
Thanks Marin, sorry I should have looked first.
0
Marin Bratanov
Telerik team
answered on 11 Jun 2019, 01:17 PM
No worries, Nick, that's why I'm here :)

I would personally have expected that to work too, in your place. We simply haven't gotten around to clearing up all of those things, with all the changes and new things that are happening. It's early days for Blazor, and even more so for component suites on it.

--Marin

0
Sean
Top achievements
Rank 1
answered on 28 Aug 2019, 06:25 PM
Hi. This shows as completed in the top left, but it doesn't work yet. Any further progress?
0
Marin Bratanov
Telerik team
answered on 29 Aug 2019, 08:29 AM

Hello Sean,

What version of Telerik UI for Blazor are you running? Can you confirm you are on 1.6.0 and that you have .NET Core 3 Preview 8 installed?

I just tested the following code and it seems to work fine for me (I am also attaching a video of my test at the end of this post):

@using Telerik.Blazor.Components.DatePicker
@using Telerik.Blazor.Components.DateInput

<TelerikDatePicker @bind-Value="theDate"></TelerikDatePicker>
<br />
The selected date is: @theDate
<br />
<TelerikDateInput @bind-Value="@StartDate" Format="MM/dd/yyyy HH:mm:ss"></TelerikDateInput>
<br />
The date input date is: @StartDate

@code {
    DateTime? theDate;
    DateTime? StartDate { get; set; } = new DateTime(2020, 1, 1);
}

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor
0
Nic
Top achievements
Rank 2
answered on 09 Dec 2020, 09:33 PM

Hello... 

Is the DatePicker having issues with nullable date values ? 

when a value is null we get the default value of 1/1/0001

how do we get around this issue .. we still want to preserve the null value..  have you guys fixed this yet? 

This post is dated from Jun 2019.. so.. i hope there is an alternative solution a year and a half later.. 

Please advice.. Thanks !!!

1
Svetoslav Dimitrov
Telerik team
answered on 11 Dec 2020, 09:30 AM

Hello,

The DatePicker should not have an issue with the nullable DateTime type. Below, I have prepared two examples of the DatPicker using both nullable and non-nullable DateTime objects. 

<h3>Nullable DateTimePicker</h3>
<TelerikDatePicker @bind-Value="@HireDate"></TelerikDatePicker>

<h3>Non-nullable DateTimePicker</h3>
<TelerikDatePicker @bind-Value="@VacationDate"></TelerikDatePicker>

@code {
    public DateTime? HireDate { get; set; } //nullable DateTime - default value null
    public DateTime VacationDate { get; set; } //non-nullable - default value 1/1/0001
}

Regards,
Svetoslav Dimitrov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Kristjan
Top achievements
Rank 1
Iron
answered on 15 May 2024, 03:32 PM
Wondering, why our loved TelerikCalendar does not enjoy the same benefits of nullable datetime as the TelerikDateInput does.
Hristian Stefanov
Telerik team
commented on 20 May 2024, 01:39 PM

Hi Kristjan,

Thank you for bringing to our attention the nullable DateTime problem within the Calendar.

To address it, I created a public item on your behalf on our public feedback portal: Calendar should accept a null value. You are automatically subscribed as a creator to receive email notifications for further status updates.

Additionally, as a token of appreciation, I have credited your account with Telerik points for reporting the behavior.

Kind Regards,

Hristian

Tags
DatePicker
Asked by
Nick
Top achievements
Rank 1
Answers by
Nick
Top achievements
Rank 1
Marin Bratanov
Telerik team
Sean
Top achievements
Rank 1
Nic
Top achievements
Rank 2
Svetoslav Dimitrov
Telerik team
Kristjan
Top achievements
Rank 1
Iron
Share this question
or