# Server Filtering of the Scheduler Events

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Scheduler</td>
 </tr>
 <tr>
  <td>Product Version</td>
  <td>Created with version 2024.4.1112</td>
 </tr>
</table>

## Description

How can I filter the events of the ASP.NET MVC Scheduler on the server based on the selected date range in the current view?

## Solution

1. Enable the [`ServerFiltering()`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/signalrdatasourcebuilder#serverfilteringsystemboolean) option of the DataSource in the Scheduler. This way, the component automatically sends a Read request to the remote endpoint upon each navigation that occurs in the Scheduler.

   ```Razor HtmlHelper
       @(Html.Kendo().Scheduler<TaskViewModel>()
           .Name("scheduler")
           ...// Additional configuration.
           .DataSource(dataSource => dataSource
               .ServerFiltering(true)
               ...// Additional configuration.
           )
       )
   ```

2. Set the [`Data()`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/crudoperationbuilder#datasystemstring) option to the Read request configuration and [add a handler that will send the start and end dates](https://docs.telerik.com/aspnet-mvc/html-helpers/datasource/getting-started#optional-pass-additional-data-to-action-methods) of the visible range of the Scheduler to the server. This will occur when the Read action is triggered.

   ```Razor HtmlHelper
       @(Html.Kendo().Scheduler<TaskViewModel>()
           .Name("scheduler")
           ...// Additional configuration.
           .DataSource(dataSource => dataSource
               .ServerFiltering(true)
               .Read(read => read.Action("Read", "Home").Data("getAdditionalData"))
               .Create("Create", "Home")
               .Destroy("Destroy", "Home")
               .Update("Update", "Home")
                   ...// Additional configuration.
           )
       )
   ```

   ```JS scripts
   <script>
       function getAdditionalData() {
           var scheduler = $("#scheduler").data("kendoScheduler"); // Get a reference to the Scheduler.

           var timezone = scheduler.options.timezone; // Get the time zone from its options.
           var startDate = kendo.timezone.convert(scheduler.view().startDate(), timezone, "Etc/UTC"); // Add the time difference between the Schduler time zone and the "Etc/UTC" time zone.
           var endDate = kendo.timezone.convert(scheduler.view().endDate(), timezone, "Etc/UTC");

           var startTime = 0;
           var endTime = 0;

           if (scheduler.view().startTime) {
               // Optionally, add startTime / endTime of the view.
               startTime = kendo.date.getMilliseconds(scheduler.view().startTime());
               endTime = kendo.date.getMilliseconds(scheduler.view().endTime());
               endTime = endTime == 0 ? kendo.date.MS_PER_DAY : endTime;
           }

           var result = {
               Start: new Date(startDate.getTime() - (startDate.getTimezoneOffset() * kendo.date.MS_PER_MINUTE) + startTime),
               End: new Date(endDate.getTime() - (endDate.getTimezoneOffset() * kendo.date.MS_PER_MINUTE) + endTime)
           }

           return result; // Pass the current "Start" and "End" dates to the server.
       }
   </script>
   ```

3. Create a `FilterRange` Model to ensure the received date range is parsed correctly. Define the setters of the `start` and `end` properties to convert the dates to UTC.

   ```C# FilterRange.cs
       using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Web;

       public class FilterRange
       {
           private DateTime start;
           public DateTime Start
           {
               get { return start; }
               set
               {
                   start = value.ToUniversalTime();
               }
           }

           private DateTime end;
           public DateTime End
           {
               get { return end; }
               set
               {
                   end = value.ToUniversalTime();
               }
           }
       }
   ```

4. Intercept the parameter of type `FilterRange` in the `Read` Action and return the filtered events data to the Scheduler.

   ```C# HomeController.cs
       public virtual JsonResult Read(DataSourceRequest request, FilterRange range)
       {
           var data = taskService.GetRange(range.Start, range.End);
           return Json(data.ToDataSourceResult(request));
       }
   ```

For a runnable example, refer to the [ASP.NET MVC application](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/SchedulerServerFiltering) in the [UI for ASP.NET MVC Examples repository](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master).

## More ASP.NET MVC Scheduler Resources

* [ASP.NET MVC Scheduler Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/scheduling/scheduler/overview)

* [ASP.NET MVC Scheduler Demos](https://demos.telerik.com/aspnet-mvc/scheduler/index)

* [ASP.NET MVC Scheduler Product Page](https://www.telerik.com/aspnet-mvc/scheduler)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Scheduler for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/scheduler)

* [Server-Side API Reference of the Scheduler for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/scheduler)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
