This is a migrated thread and some comments may be shown as answers.

Setting filters to grid on server side

5 Answers 1222 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ilkka
Top achievements
Rank 1
Ilkka asked on 10 Jul 2013, 08:20 AM
Hi,

I am using kendo grid and trying to set  filters beforehand when grid is constructed. I am setting the filters for datasource. The problem is that when grid is rendered I always encounter the problem, exception is throws. Call stack is following.

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=Provided expression should have string type
Parameter name: stringExpression
  Source=Kendo.Mvc
  ParamName=stringExpression
  StackTrace:
       at Kendo.Mvc.Infrastructure.Implementation.Expressions.ExpressionFactory.LiftStringExpressionToEmpty(Expression stringExpression)
       at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterOperatorExtensions.GenerateToLowerCall(Expression stringExpression, Boolean liftMemberAccess)
       at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterOperatorExtensions.GenerateCaseInsensitiveStringMethodCall(MethodInfo methodInfo, Expression left, Expression right, Boolean liftMemberAccess)
       at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterOperatorExtensions.GenerateStartsWith(Expression left, Expression right, Boolean liftMemberAccess)
       at Kendo.Mvc.Infrastructure.Implementation.Expressions.FilterOperatorExtensions.CreateExpression(FilterOperator filterOperator, Expression left, Expression right, Boolean liftMemberAccess)
       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 enumerable, DataSourceRequest request)
       at Kendo.Mvc.UI.DataSource.Process(DataSourceRequest request, Boolean processData)
       at Kendo.Mvc.UI.Grid`1.ProcessDataSource()
       at Kendo.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer)
       at Kendo.Mvc.UI.WidgetBase.Render()
       at Kendo.Mvc.UI.Fluent.WidgetBuilderBase`2.Render()
       at xxxxxxxx.Execute() in xxxxxxxx.cshtml:line xx
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at RazorGenerator.Mvc.PrecompiledMvcView.Render(ViewContext viewContext, TextWriter writer)
       at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
  InnerException:

Any ideas how to proceed?

5 Answers, 1 is accepted

Sort by
0
Accepted
Ilkka
Top achievements
Rank 1
answered on 10 Jul 2013, 10:32 AM
I found the solution. Kendo.Mvc.FilterDescriptor constructor omits Type parameter. It needs to be set manually.

new FilterDescriptor( ... ) {MemberType = typeof (string)}

It would be nice, if MemberType could be set with constructor parameter as well.


0
Lasse
Top achievements
Rank 1
answered on 19 Nov 2014, 09:34 AM
Where do you set the member type?

I have an action method like this:

public ActionResult GridData([DataSourceRequest]DataSourceRequest request)
{
    return Json(GetOrders().ToDataSourceResult(request));
}
0
Alexander Popov
Telerik team
answered on 21 Nov 2014, 09:52 AM
Hi Mikkel,

Here is an example how you can add a filter on the server-side: 
public ActionResult EditingInline_Read([DataSourceRequest] DataSourceRequest request)
{
    request.Filters.Add(new FilterDescriptor() { Member = "ProductName", MemberType = typeof(string), Operator = FilterOperator.IsEqualTo, Value = "Chai" });
    return Json(productService.Read().ToDataSourceResult(request));
}

Regards,
Alexander Popov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
-1
Chris
Top achievements
Rank 1
answered on 24 Apr 2019, 06:08 PM
Why would you set it on server side like that? You're just hardcoding data.  worthless example.
0
Georgi
Telerik team
answered on 26 Apr 2019, 09:04 AM
Hi Chris,

The example provided by Alexander aims to demonstrate how to add a filter on the server. As shown in his example this can be done by creating a FilterDescriptor object and adding it to the Filters collection of the DataSourceRequest parameter. Although in his sample everything is hardcoded for simplicity, you can create a method which dynamically creates the filter criterion and adds it to the filters collection.


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Ilkka
Top achievements
Rank 1
Answers by
Ilkka
Top achievements
Rank 1
Lasse
Top achievements
Rank 1
Alexander Popov
Telerik team
Chris
Top achievements
Rank 1
Georgi
Telerik team
Share this question
or