Unable to cast object of type 'Kendo.Mvc.CompositeFilterDescriptor' to type 'Kendo.Mvc.FilterDescriptor'

2 posts, 0 answers
  1. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 10 Dec 2014 Link to this post

    I am trying to alter the meber name of a filterdescriptor but get error Unable to cast object of type 'Kendo.Mvc.CompositeFilterDescriptor' to type 'Kendo.Mvc.FilterDescriptor'  here

    If request.Filters.Any(Function(y) CType(y, Kendo.Mvc.FilterDescriptor).Member.Equals("FranchiseeName")) Then
     
                         Dim filter As FilterDescriptor = CType(request.Filters.Single(Function(g) CType(g, Kendo.Mvc.FilterDescriptor).Member.Equals("FranchiseeName")), FilterDescriptor)
                         filter.Member = "Franchisee.Name"
     
     
                     End If

    I don't know what the difference is or why I get this, I have done simular before without this problem.
    Thanks

  2. Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 12 Dec 2014 Link to this post

    Hi Alan,

    There are two concreate implementation of IFilterDescriptor - the CompositeFilterDescriptor and the FilterDescriptor. CompositeFilterDescriptor class is required as the filters can be complex - multiple filters with And or Or logic. Therefore, usually the FilterDescriptors are nested within an instance of CompositeFilterDescriptor. Therefore in order to iterate over the filters you will need to conditionally navigate to through the CompositeFilterDescriptor's FilterDescriptors property. Similar to the following:

    private void ChangeMemberName(IEnumerable<IFilterDescriptor> filters, string memberToSearchFor, string newMemberName)
    {
        foreach (var filter in filters)
        {
            if (filter is CompositeFilterDescriptor)
            {
                ChangeMemberName(((CompositeFilterDescriptor)filter).FilterDescriptors, memberToSearchFor, newMemberName);
            }
            else if (((FilterDescriptor)filter).Member == memberToSearchFor)
            {
                ((FilterDescriptor)filter).Member = newMemberName;
            }
        }
    }
     
    public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request)
    {
        if (request.Filters.Any())
        {
            ChangeMemberName(request.Filters, "FranchiseeName", "Franchisee.Name");
        }
     
       /*..*/
    }


    Regards,
    Rosen
    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.

     
Back to Top