PivotGrid in Razor Pages
This article describes how to seamlessly integrate and configure the Telerik UI PivotGrid for ASP.NET Core in Razor Pages applications.
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
The following example demonstrates how to configure the PivotGrid in a Razor Pages scenario and bind it to a remote data collection.
-
Define the PivotGrid and specify the Read request URL in the
DataSourceconfiguration. The URL must refer to the method name in thePageModel.Razor@page @model IndexModel @using Kendo.Mvc.UI <div class="k-pivotgrid-wrapper"> @(Html.Kendo().PivotConfigurator() .Name("configurator") .Filterable(true) .Height(500) ) @(Html.Kendo().PivotGrid<CustomerViewModel>() .Name("pivotgrid") .Configurator("#configurator") .ColumnWidth(120) .Filterable(true) .Height(500) .DataSource(dataSource => dataSource .Ajax() .Transport(transport => transport.Read(r => r.Url(Url.Page("Index", "Read")).Data("forgeryToken"))) .Schema(schema => schema .Cube(cube => cube .Dimensions(dimensions => { dimensions.Add(model => model.ContactName).Caption("All Contacts"); dimensions.Add(model => model.CompanyName).Caption("All Companies"); dimensions.Add(model => model.Country).Caption("All Countries"); dimensions.Add(model => model.ContactTitle).Caption("All Titles"); }) .Measures(measures => measures.Add("Contacts Count").Field(model => model.CustomerID).AggregateName("count")) ) ) .Columns(columns => { columns.Add("Country").Expand(true); columns.Add("CompanyName"); }) .Rows(rows => rows.Add("ContactTitle").Expand(true)) .Measures(measures => measures.Values("Contacts Count")) ) ) </div> -
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 the Read data operation.C#public class IndexModel : PageModel { public static IList<CustomerViewModel> customers; public void OnGet(string culture) { if (!String.IsNullOrEmpty(culture)) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(culture); } if (customers == null) { customers = new List<CustomerViewModel>(); Enumerable.Range(1, 50).ToList().ForEach(i => customers.Add(new CustomerViewModel { CustomerID = "CustomerID " + i, CompanyName = "CompanyName " + (i % 3), ContactName = "ContactName " + i, ContactTitle = "ContactTitle " + (i % 7), Country = "Country " + (i % 3) })); } } public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request) { return new JsonResult(customers.ToDataSourceResult(request)); } }