# Validate Timeslot Availability against Existing Recurrence Rules

## 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 validate if the Timeslot of a new event is available based on the existing recurrence rules of the ASP.NET MVC Scheduler?

## Solution

The following example shows how to validate server-side when a new event is about to be created. If the validation fails, the event is not saved, and the user receives an error message that the selected time does not have available slots.

1. Validate the data of the new event in the Create action based on your requirements, and add a [ModelState error](https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.modelstatedictionary.addmodelerror?view=aspnet-mvc-5.2) if the current timeslot is not available. Return a collection with the created event and pass the ModelState to the [`ToDataSourceResult()`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.extensions/queryableextensions#todatasourceresultsystemcollectionsienumerablekendomvcuidatasourcerequestmicrosoftaspnetcoremvcmodelbindingmodelstatedictionary) method.

   ```C#
       public virtual JsonResult Meetings_Create([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting)
       {
           if (ModelState.IsValid)
           {
               if (CheckEventSlot(meeting)) // Validate the timeslot availability.
               {
                   meetingService.Insert(meeting, ModelState);
               }
               else
               {
                   ModelState.AddModelError("start", "The new event is not allowed because the timeslot is unavailable.");
               }
           }
           return Json(new[] { meeting }.ToDataSourceResult(request, ModelState));
       }

       private bool CheckEventSlot(MeetingViewModel newMeeting)
       {
           foreach (var meeting in meetingService.GetAll())
           {
               if (!string.IsNullOrEmpty(meeting.RecurrenceRule))
               {
                   var start = meeting.Start;
                   var end = meeting.End;
                   var recRule = meeting.RecurrenceRule;

                   var condition = false; // Here you can use 3rd party software like iCal.Net to build up the condition.
                   if (condition)
                   {
                       return false;
                   }
               }
           }
           return true;
       }
   ```

2. Handle the [`Error`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/datasourceeventbuilder#errorsystemstring) event of the Scheduler DataSource and display the error message to the user.

   ```Razor HtmlHelper
           @(Html.Kendo().Scheduler<MeetingViewModel>()
               .Name("scheduler")
               ...// Additional configuration.
               .DataSource(d => d
                   .Events(e => e.Error("onError"))
                   ...// Additional configuration.
               )
           )
   ```

   ```JS
   <script type="text/javascript">
       function onError(args) { // The event will fire when there are Model State errors.
           var errors = args.errors; // Get the received errors.
           var message = "";
           var scheduler = $("#scheduler").data("kendoScheduler");
           if (errors) {
               scheduler.one("dataBinding", function (e) { // Subscribe to the "dataBinding" event of the Scheduler once.
                   e.preventDefault(); // Prevent the default event action.
                   $.each(errors, function (key, value) { // Loop through the errors.
                       if ('errors' in value) {
                           $.each(value.errors, function() {
                               message += this + "\n"; // Store the messages in a variable.
                           });
                       }
                   });
                   alert(message); // Display the message.
               });
           }
       }
   </script>
   ```

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/SchedulerValidatingTimeslot) 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)
