ToDataSourceResult() throws InvalidCastException when filtering on a DateOnly property

1 Answer 97 Views
Grid
Bill
Top achievements
Rank 2
Iron
Bill asked on 05 Aug 2024, 09:28 PM

In 2024.1 and 2024.2 (perhaps other versions, but those are the two I tried), using ToDataSourceResult to filter on a property whose type is DateOnly results in an InvalidCastException.  This is due to ToDataSourceResult using .NET's Convert.DefaultToType which does not support converting to DateOnly.  Per https://github.com/dotnet/runtime/issues/73074 (which is about a different but related method), the team at Microsoft is adamant about not adding the support because the Convert methods have fallen out of vogue.  As a workaround, I wrote a method to scan through the filter tree of the DataSourceRequest and pre-convert any value corresponding to a DateOnly property from a string or DateTime to a DateOnly so that it will not need to be converted within ToDataSourceResult, but that is quite tedious since I have to do that to each of my applications and call it before every call to ToDataSourceResult.  It would be nice if ToDataSourceResult could handle this case internally.

 

For reference, here is the relevant part of the stack trace:

at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterDescriptorExpressionBuilder.CreateValueExpression(Type targetType, Object value, CultureInfo culture)
   at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterDescriptorExpressionBuilder.CreateBodyExpression()
   at Kendo.Mvc.FilterDescriptor.CreateFilterExpression(ParameterExpression parameterExpression)
   at Kendo.Mvc.FilterDescriptorBase.CreateFilterExpression(Expression instance)
   at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterDescriptorCollectionExpressionBuilder.CreateBodyExpression()
   at Kendo.Mvc.CompositeFilterDescriptor.CreateFilterExpression(ParameterExpression parameterExpression)
   at Kendo.Mvc.FilterDescriptorBase.CreateFilterExpression(Expression instance)
   at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterDescriptorCollectionExpressionBuilder.CreateBodyExpression()
   at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterExpressionBuilder.CreateFilterExpression()
   at Kendo.Mvc.Extensions.QueryableExtensions.Where(IQueryable source, IEnumerable`1 filterDescriptors)
   at Kendo.Mvc.Extensions.QueryableExtensions.CreateDataSourceResult[TModel,TResult](IQueryable queryable, DataSourceRequest request, ModelStateDictionary modelState, Func`2 selector)
   at Kendo.Mvc.Extensions.QueryableExtensions.ToDataSourceResult(IQueryable queryable, DataSourceRequest request, ModelStateDictionary modelState)
   at Kendo.Mvc.Extensions.QueryableExtensions.ToDataSourceResult(IQueryable queryable, DataSourceRequest request)

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 08 Aug 2024, 10:06 AM

Hello Bill,

The issue would most notably be caused by the fact that the Telerik UI for ASP.NET Core suite does not offer built-in support for DateOnly fields.

There is a Feature Request on the support for DateOnly with the DatePicker components:

Feedback Portal Item - https://feedback.telerik.com/aspnet-core-ui/1594258-dateonly-type-support

Feel free to cast your vote to increase the item's priority.

For the Telerik UI for ASP.NET Core suite, we have not given any exact estimates on whether such a feature could be potentially introduced. However, I see that there is a rising trend for this feature and have raised to my manager in order to prioritize it in our roadmaps.

 

Regards,
Mihaela
Progress Telerik

Do you have a stake in the designеr-developer collaboration process? If so, take our survey to share your perspective and become part of this global research. You’ll be among the first to know once the results are out.
-> Start The State of Designer-Developer Collaboration Survey 2024

Tags
Grid
Asked by
Bill
Top achievements
Rank 2
Iron
Answers by
Mihaela
Telerik team
Share this question
or