Scheduler in Razor Pages
This article describes how to seamlessly integrate and configure the Telerik UI Loader for ASP.NET Core in Razor Pages applications.
You can use any of the available data binding approaches to bind the component to data in a Razor Pages application.
Referencing Handler Methods in Razor Pages
Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml file and a cshtml.cs file (by convention, the two files have the same name).
The cshtml.cs file, known as the PageModel, contains handler methods that respond to HTTP requests. These methods are prefixed with On followed by the HTTP verb (for example, OnGet, OnPost, OnPostRead, OnPostCreate).
Handler methods declared in a PageModel can be referenced from any Razor Page using one of the following URL patterns:
-
Using
Url.Page()C#Url.Page("PageName", "HandlerName") // OR Url.Page("/FolderName/PageName", "HandlerName")For example,
Url.Page("Index", "Read")references theOnPostReadorOnGetReadhandler method in theIndex.cshtml.csfile. -
Using a query string
C#Url("/PathToPage?handler=HandlerName")For example,
Url("/Index?handler=Read")references theOnPostReadorOnGetReadhandler method in theIndexpage.
For more information on Razor Pages architecture and concepts, refer to the official Microsoft documentation.
Binding to Remote Data
To configure the CRUD operations of the Scheduler DataSource within a Razor Pages application, follow the next steps:
-
Specify the
Read,Create,Update, andDestroyoptions of theDataSourceconfiguration. The URL in each of these options must refer to the method name in thePageModel.Razor@page @model IndexModel @(Html.Kendo().Scheduler<MeetingViewModel>() .Name("scheduler") .Timezone("Etc/UTC") .DataSource(d => d .Model(m => { m.Id(f => f.MeetingID); m.Field(f => f.Title).DefaultValue("No title"); m.Field(f => f.Start); m.Field(f => f.End); m.Field(f => f.Description); m.Field(f => f.RecurrenceID); m.Field(f => f.RecurrenceRule); m.Field(f => f.RecurrenceException); m.Field(f => f.StartTimezone); m.Field(f => f.EndTimezone); m.Field(f => f.IsAllDay); }) .Read(r => r.Url(Url.Page("Index", "Meetings_Read")).Data("forgeryToken")) .Create(c => c.Url(Url.Page("Index", "Meetings_Create")).Data("forgeryToken")) .Destroy(d => d.Url(Url.Page("Index", "Meetings_Destroy")).Data("forgeryToken")) .Update(u => u.Url(Url.Page("Index", "Meetings_Update")).Data("forgeryToken")) ) ... // Additional configuration options. ) -
Add an
AntiForgeryTokenat the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith the CRUD requests.JS<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>Additional parameters can also be supplied.
JS<script> function forgeryToken() { return { __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken, additionalParameter: "test" } } </script> -
Within the
cshtml.csfile, add a handler method for each data operation.C#public class IndexModel : PageModel { public static IList<MeetingViewModel> meetings; public void OnGet() { if (meetings == null) { // Populate the "meetings" collection with data (events). meetings = new List<MeetingViewModel>(); Enumerable.Range(1, 5).ToList().ForEach(x => meetings.Add(new MeetingViewModel() { MeetingID = x, Title = "Event " + x, Start = DateTime.Now.AddHours(x * 2), End = DateTime.Now.AddHours(x * 3), Description = "Description for event " + x })); } } public virtual JsonResult OnPostMeetings_Read([DataSourceRequest] DataSourceRequest request) { return new JsonResult(meetings.ToDataSourceResult(request)); } public virtual JsonResult OnPostMeetings_Create([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting) { if (ModelState.IsValid) { meeting.MeetingID = meetings.Count + 2; meetings.Add(meeting); } return new JsonResult(new[] { meeting }.ToDataSourceResult(request, ModelState)); } public virtual JsonResult OnPostMeetings_Destroy([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting) { if (ModelState.IsValid) { meetings.Remove(meetings.First(x => x.MeetingID == meeting.MeetingID)); } return new JsonResult(new[] { meeting }.ToDataSourceResult(request, ModelState)); } public virtual JsonResult OnPostMeetings_Update([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting) { if (ModelState.IsValid) { meetings.Where(x => x.MeetingID == meeting.MeetingID).Select(x => meeting); } return new JsonResult(new[] { meeting }.ToDataSourceResult(request, ModelState)); } }
For the complete project, refer to the Scheduler in Razor Pages example.