Provided expression should have string type (Parameter 'stringExpression')

1 Answer 247 Views
Grid
RickC
Top achievements
Rank 1
RickC asked on 10 Feb 2022, 12:59 PM | edited on 10 Feb 2022, 01:04 PM

I'm using Kendo UI for aspnet core MVC. And whenever I use the search with a numeric value I get an exception:

Provided expression should have string type (Parameter 'stringExpression')

It works however great with strings. So for example: "test" works but 5 throws an exception.

My search in my grid is configured like this


.Search(s => { 
                    s.Field(c => c.IncidentEvent, "contains"); 
                    s.Field(c => c.CourseNumber, "contains"); 
                    s.Field(c => c.CourseCode, "contains");
                    s.Field(c => c.ActivityType, "contains");
                    s.Field(c => c.SeverityType, "contains");

All my fields are of string type. However I expect kendo grid to be able to parse integers as well when doing a search.

Is there a way around this? How can I get my search to work with numbers as well?


RickC
Top achievements
Rank 1
commented on 10 Feb 2022, 01:22 PM

I tried something based on this answer:

https://www.telerik.com/forums/grid-filter-on-guid-provided-expression-should-have-string-type

When I use this function to set the membertype it works:

        private void ModifyFilters(IList<IFilterDescriptor> filters)
        {
            if (filters.Any())
            {
                foreach (var filter in filters)
                {
                    var descriptor = filter as Kendo.Mvc.FilterDescriptor;
                    if (descriptor != null)
                    {
                        descriptor.Value = descriptor.Value.ToString();
                        descriptor.MemberType = typeof(string);
                    }
                    else if (filter is CompositeFilterDescriptor)
                    {
                        ModifyFilters(((CompositeFilterDescriptor)filter).FilterDescriptors);
                    }
                }
            }
        }
But this is far from ideal. I don't want to use this workaround on every search in my application. There has to be another fix for this?

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 15 Feb 2022, 10:26 AM

Hello RickC,

If you want to use the Grid's search panel with a field of type int, you should set a supported filter operator for that field, e.g, "eq" (is equal to):

.Search(s =>
{
    s.Field(o => o.Freight, "eq");
    s.Field(o => o.ShipName, "contains");
    s.Field(o => o.ShipCity, "contains");
})

You can use "contains" for fields of type string. Using it on an int field will result in an exception.

We don't have any workaround to suggest, other than the one you posted, which modifies the filters on the server.

Regards,
Ivan Danchev
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/.

Tags
Grid
Asked by
RickC
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or