Chart Wizard 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 Chart Wizard for ASP.NET Core in Razor Pages applications.
This article describes how to configure the Chart Wizard component in a Razor Pages scenario.
Getting Started
To connect the Chart Wizard to a data set retrieved from a remote endpoint in a Razor Pages application, proceed with the following steps:
-
Specify the Read request URL in the
DataSourceconfiguration. The URL must refer to the method name in thePageModel.Razor@page @model IndexModel @(Html.Kendo().ChartWizard<Product>() .Name("chartwizard") .DataSource(dataSource => dataSource .Read(r => r.Url("/Index?handler=Read").Data("forgeryToken")) ) .DataColumns(columns => { columns.Add().Field(f => f.ProductName).Title("Product Name"); columns.Add().Field(f => f.Quantity); }) ) -
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 static List<Product> products; public void OnGet(string culture) { if (!String.IsNullOrEmpty(culture)) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(culture); } if (products == null) { products = GetData(); } } public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request, int? id) { return new JsonResult(products.ToDataSourceResult(request)); } private static List<Product> GetData() { return new List<Product>() { new Product { ProductID = 216321, ProductName = "Calzone", Quantity = 1 }, new Product { ProductID = 546897, ProductName = "Margarita", Quantity = 2 }, new Product { ProductID = 456231, ProductName = "Pollo Formaggio", Quantity = 1 } }; }