I am using the RadzenDatePicker
to get time value only. I have managed to make it show only time instead of the calendar, however, I can't bind it's value which is DateTime
to TimeOnly
.
Is there a way I can bind the RadzenDatePicker
below to TimeOnly
property?
Code:
<RadzenDatePicker style="display: block; width: 100%"
@bind-Value="@station.OpenTime"
Name="OpenTime"
TValue="TimeOnly"
ShowTime="true"
HourFormat="12"
TimeOnly="true"/>
Control:
Error:
System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.TimeOnly'.
at Radzen.Blazor.RadzenDatePicker`1.OnChange()
at Radzen.Blazor.RadzenDatePicker`1.OkClick()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Blazor databinding is type-safe so this won't work directly. The
TValue
parameter is a bit of a lie. It will only accept DateTime and maybe DateTimeOffset.So you will need a DateTime value to bind to and then you can update your target with the Change event:
When you need this a lot you could wrap the Radzen component with one of your own.