dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Blazor - InputDate crash with Nullable<DateTime> #18096

Closed anderjoy closed 4 years ago

anderjoy commented 4 years ago

Describe the bug

InputDate changes to null when entering a new date starting with zero (0).

For example, assuming the user enters 2020/01/02 and then decides to change the month to 02, the property is changed to null.

BlazorAppDateTime

I think the problem is on this line: https://github.com/aspnet/AspNetCore/blob/c7937640a4079465d36441724933eae5a9ca0085/src/Components/Web/src/Forms/InputBase.cs#L88

When _nullableUnderlyingType == typeof (DateTime) the problem occurs because CurrentValue is set to default (null).

In my tests, I changed this line to:

if (_nullableUnderlyingType != null && _nullableUnderlyingType != typeof(DateTime) && string.IsNullOrEmpty(value))

And the problem has been resolved.

To Reproduce

Create a Blazor Server-Side Project, change the index.razor for:

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

<EditForm Model="people">
    <InputDate @bind-Value="@people.Birthday"></InputDate>

    @if (people.Birthday == null)
    {
        <pre>null</pre>
    }
    else
    {
        <h3>@people.Birthday</h3>
    }
</EditForm>

@code {

    private People people = new People();

    public class People
    {
        public Nullable<DateTime> Birthday { get; set; }
    }
}

Further technical details

Ambiente de runtime: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.100\

Host (useful for support): Version: 3.1.0 Commit: 65f04fb6db

.NET Core SDKs installed: 2.2.401 [C:\Program Files\dotnet\sdk] 3.1.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

pranavkm commented 4 years ago

I don't see a crash here. However nullable types do get resetted to default when the value is empty which happens to the value browsers use for invalid date times.

Nullable values get resetted to default, while non-nullable types retain the previous value if parsing fails: https://github.com/aspnet/AspNetCore/blob/c7937640a4079465d36441724933eae5a9ca0085/src/Components/Web/src/Forms/InputBase.cs#L94. @SteveSandersonMS would you happen to know why we made that choice?

SteveSandersonMS commented 4 years ago

It’s because empty is considered a valid value for a nullable datetime, but not for a regular one. So we can only accept it in the former case.

anderjoy commented 4 years ago

I understood... Thanks

ghost commented 4 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.