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

Parse DataSourceRequest object and pass to API

1 Answer 86 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Kris
Top achievements
Rank 1
Kris asked on 28 May 2020, 11:53 PM

What's the best way to pass a filter to an API in order to filter data from a database?

To elaborate: my team is creating a web portal in order to monitor OS deployments.  We want the user to be able to filter data which is stored on a Sql database.  We plan on using a Kendo Chart to visualize the data and filter the data using a Kendo Filter.  We have also created an API to perform our http operations.  I know that the filter results are passed to the controller in the form of a DataSourceRequest object.  This gets to my question, what is the best way to pass the settings a user picks on the front-end to the API/database?  Can I parse the DataSourceRequest object?

Let me know if anything needs clarifying.

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 02 Jun 2020, 02:09 PM

Hi Kris,

You could use the ToDataSourceResult method which will filter/sort/page/aggregate/group using the settings from the DataSourceRequest parameter.

e.g.

public ActionResult Select([DataSourceRequest]DataSourceRequest request) // request contains the filter query
{
    var data = Enumerable.Range(1, 10)
        .Select(index => new Product
        {
            ProductID = index,
            ProductName = "Product #" + index,
            UnitPrice = index * 10,
            Discontinued = false
        });

    return Json(data.ToDataSourceResult(request), JsonRequestBehavior.AllowGet); // ToDataSourceResult filters the data using the provided query
}

Regards,
Georgi
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Filter
Asked by
Kris
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or