radzenhq / radzen-blazor

Radzen Blazor is a set of 90+ free native Blazor UI components packed with DataGrid, Scheduler, Charts and robust theming including Material design and FluentUI.
https://www.radzen.com
MIT License
3.52k stars 785 forks source link

DatePicker throws System.InvalidCastException When Entering Valid Date Manually and Hit Enter #257

Closed nlpMbordogna closed 2 years ago

nlpMbordogna commented 2 years ago

[2021-10-20T19:41:52.472Z] Error: System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.Nullable1[System.DateTimeOffset]'. at Radzen.Blazor.RadzenDatePicker1.ParseDate() at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle) e.log @ blazor.server.js:21

html brackets removed so it the code can show in Git

` RadzenDatePicker TValue="DateTimeOffset?" DateFormat="@this.FormatMask" ValueChanged="@(async (value) => { await this.ValueChanged(value); })" Value="@this.LowerBoundValue" Name="from_date" /

@code{ public DateTimeOffset? LowerboundValue { get; set; }

public string FormatMask {get; set;} = "MM/dd/yyyy";

public Task ValueChanged(DatetimeOffset? val){
// do something
}

} `

enchev commented 2 years ago

I'm unable to reproduce such exception using our demos with the same configuration: image image

You can attach the source code to debug the exception at your end.

nlpMbordogna commented 2 years ago

Actually that isn't the same setup....

  1. Set TValue="DateTimeOffset?"
  2. you are using bind- not the separate Value and ValueChange event (which i know bind is syntactical sugar for the same thing but please keep your test apples to apples). I looked at the source in the parse method. if you configure it the same way then it will blow up

Another note is your event is sending back datetime not datetime offset, this is because your TValue isn't set to DatetimeOffset?. its a minor fix to do the check and the proper cast based on TValue.