PivotGrid 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 design, the two files have the same name).
You can seamlessly integrate the Telerik UI PivotGrid for ASP.NET Core in Razor Pages applications.
This article describes how to configure the PivotGrid component in a Razor Pages scenario.
Getting Started
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
DataSource
configuration. 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("/Index?handler=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
AntiForgeryToken
at the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken()
-
Send the
AntiForgeryToken
with 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.cs
file, add a handler method for the Read data operation.C#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)); }