Gantt 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 Gantt DataSource within a Razor Pages application, follow the next steps:
-
Specify the
Read,Create,Update, andDestroyoptions of theDataSourceconfigurations for the tasks and dependencies. The URL in each of these options must refer to the method name in thePageModel.Razor@page @model IndexModel @(Html.Kendo().Gantt<TaskViewModel, DependencyViewModel>() .Name("gantt") .Columns(columns => { columns.Bound(c => c.TaskID).Title("ID").Width(50); columns.Bound(c => c.Title).Editable(true).Sortable(true); columns.Group(g => { g.Bound(c => c.Start).Width(100).Editable(true).Sortable(true); g.Bound(c => c.End).Width(100).Editable(true).Sortable(true); }).Title("Timings"); }) .Views(views => { views.DayView(); views.WeekView(weekView => weekView.Selected(true)); views.MonthView(); }) .Height(700) .ShowWorkHours(false) .ShowWorkDays(false) .Snap(false) .DataSource(d => d .Model(m => { m.Id(f => f.TaskID); m.ParentId(f => f.ParentID); m.Field(f => f.Expanded).DefaultValue(true); }) .Read(r => r.Url(Url.Page("Index", "Read")).Data("forgeryToken")) .Create(r => r.Url(Url.Page("Index", "Create")).Data("forgeryToken")) .Update(r => r.Url(Url.Page("Index", "Update")).Data("forgeryToken")) .Destroy(r => r.Url(Url.Page("Index", "Destroy")).Data("forgeryToken")) ) .DependenciesDataSource(d => d .Model(m => { m.Id(f => f.DependencyID); m.PredecessorId(f => f.PredecessorID); m.SuccessorId(f => f.SuccessorID); }) .Read(r => r.Url(Url.Page("Index", "DependenciesRead")).Data("forgeryToken")) .Create(r => r.Url(Url.Page("Index", "DependenciesCreate")).Data("forgeryToken")) .Update(r => r.Url(Url.Page("Index", "DependenciesUpdate")).Data("forgeryToken")) .Destroy(r => r.Url(Url.Page("Index", "DependenciesDestroy")).Data("forgeryToken")) ) ) -
Add an
AntiForgeryTokenat the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith the Read request.JavaScript<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>Additional parameters can also be supplied.
JavaScript<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 JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request) { return new JsonResult(tasks.ToDataSourceResult(request)); } public JsonResult OnPostCreate([DataSourceRequest] DataSourceRequest request, TaskViewModel task) { task.TaskID = Guid.NewGuid().ToString(); if (ModelState.IsValid) { tasks.Add(task); } return new JsonResult(new[] { task }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, TaskViewModel task) { int index = tasks.IndexOf(tasks.FirstOrDefault(item => { return item.TaskID == task.TaskID; })); tasks[index] = task; return new JsonResult(new[] { task }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostDestroy([DataSourceRequest] DataSourceRequest request, TaskViewModel task) { int index = tasks.IndexOf(tasks.FirstOrDefault(item => { return item.TaskID == task.TaskID; })); tasks.RemoveAt(index); return new JsonResult(new[] { task }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostDependenciesRead([DataSourceRequest] DataSourceRequest request) { return new JsonResult(dependencies.ToDataSourceResult(request)); } public JsonResult OnPostDependenciesCreate([DataSourceRequest] DataSourceRequest request, DependencyViewModel dependency) { if (ModelState.IsValid) { dependencies.Add(dependency); } return new JsonResult(new[] { dependency }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostDependenciesUpdate([DataSourceRequest] DataSourceRequest request, DependencyViewModel dependency) { int index = dependencies.IndexOf(dependencies.FirstOrDefault(item => { return item.DependencyID == dependency.DependencyID; })); dependencies[index] = dependency; return new JsonResult(new[] { dependency }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostDependenciesDestroy([DataSourceRequest] DataSourceRequest request, DependencyViewModel dependency) { int index = dependencies.IndexOf(dependencies.FirstOrDefault(item => { return item.DependencyID == dependency.DependencyID; })); dependencies.RemoveAt(index); return new JsonResult(new[] { dependency }.ToDataSourceResult(request, ModelState)); }
For the complete project, refer to the Gantt in Razor Pages example.