How to access DataSourceRequest.Filters in Controller?

5 posts, 0 answers
  1. Robert
    Robert avatar
    23 posts
    Member since:
    Jun 2012

    Posted 21 Mar 2013 Link to this post

    Hi there,

    in my case, I have a ViewModel on the client side. It works and shows the data from the "real" model. The real model consists of an object that contains other objects, e.g. ArchivedDoubleValue refers to a Station, which has a name, via its StationId. In my ViewModel, things are flattened in order to be fast and avoid circular dependencies, so there is:

    int Id (ArchivedDoubleValue.Id)
    double Value (ArchivedDoubleValue.Value)
    string StationName (refers to Station.Name and gets set in the controller)

    Now, when I want to sort or filter the StationName column, I run into an error, because StationName does not exist in the "real" model, only StationId.

    So I thought I could simply "translate" the Member String in DataSourceRequest.Groups and DataSourceRequest.Sorts back into the corresponding Id. That worked quite well:

    When grouping in the client, the request.Groups contains an entry with its Member set to "StationName". This is just a string and I change it to the corresponding StationId. Same works well for sorting.

    But it DOES NOT work for filtering!  When debugging and peeking into he request at runtime, I can see and change the Filters Members and Values, but when I try to change it via code in the controller, I can not access it:

                if (request.Filters.Count > 0)
                {
                    foreach (var f in request.Filters)
                    {
                        String fs = f.Member.ToString();

    This gives me an error:
    Kendo.Mvc.IFilterDescriptor does not contain a definition for "Member"

    How am I supposed to filter the "real" data with the filter set to a column in my ViewModel? I wonder how to do that? Or is there a way to access request.Filters? I think that would be the most elegant way to do it as it wiould be transparent to the client.

    Please help me with that, thanks in advance!

    Regards, Rob
  2. Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 22 Mar 2013 Link to this post

    Hello Robert,

    The Filter property is a collection of the IFilterDescriptor. This is the base interface which the concrete FilterDescriptor types implement. Therefore, you should cast the item of this collection to the appropriate type before accessing its members:

    private void ModifyFilters(IEnumerable<IFilterDescriptor> filters)
    {
        if (filters.Any())
        {
            foreach (var filter in filters)
            {
                var descriptor = filter as FilterDescriptor;
                if (descriptor != null && descriptor.Member == "SomeField")
                {
                    descriptor.Member = "SomeOtherField";
                }
                else if (filter is CompositeFilterDescriptor)
                {
                    ModifyFilters(((CompositeFilterDescriptor)filter).FilterDescriptors);
                }
            }
        }
    }
     
    public ActionResult _Read([DataSourceRequest] DataSourceRequest request)
    {
        ModifyFilters(request.Filters);
     
         //....
    }

    Greetings,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Gerry
    Gerry avatar
    7 posts
    Member since:
    Sep 2012

    Posted 27 Feb 2014 in reply to Rosen Link to this post

    Thank you Rosen for posting this little recursive function.  I have been searching all day for this solution.

    Gerry
  4. Ajay
    Ajay avatar
    10 posts
    Member since:
    Mar 2013

    Posted 25 Sep 2014 in reply to Rosen Link to this post

    Hi Rosen,

    I have a weird situation where I have 5 filterable columns on my grid and have a filter button outside my grid. I need to specify the filters and have the grid not filter right away. The user then clicks the Filter button and then i need to pass my filters to the controller. How do I do that and how do i iterate through the filter collection and set filter values to a concrete filter object :
    public class GridFilters
    {
        public string FieldName {get;set;}....   Thanks
  5. Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 29 Sep 2014 Link to this post

    Hello Ajay,

    I'm afraid that it is not clear what your scenario is. If you want to set DataSource filters manually you should use its filter method. If server operations option is enabled the DataSource will send the filter descriptors to the server for processing. If you need to pass some additional data with the request, this can be done by assigning a JavaScript function as described in this help article.

    If you continue to experiencing difficulties and as your question does not seems to be directly related to this thread's topic, I would suggest you to open a separate support request and provide a small runnable sample which to demonstrate your exact scenario and the issue you are facing.

    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