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

How to access DataSourceRequest.Filters in Controller?

4 Answers 4459 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 21 Mar 2013, 12:05 PM
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

4 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 22 Mar 2013, 08:45 AM
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!
0
Gerry
Top achievements
Rank 1
answered on 27 Feb 2014, 11:14 PM
Thank you Rosen for posting this little recursive function.  I have been searching all day for this solution.

Gerry
0
Ajay
Top achievements
Rank 1
answered on 25 Sep 2014, 11:53 PM
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
0
Rosen
Telerik team
answered on 29 Sep 2014, 02:37 PM
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.

 
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Gerry
Top achievements
Rank 1
Ajay
Top achievements
Rank 1
Share this question
or