Sparkline 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 Sparkline for ASP.NET Core in Razor Pages applications.
This article describes how to configure a basic Sparkline component in a Razor Pages scenario.
For the complete project, refer to the Sparkline in Razor Pages example.
Getting Started
The most flexible form of data binding is to use the DataSource component. To bind the Sparkline to a data set received from a remote endpoint within a Razor Pages application, follow the next 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().Sparkline() .Name("sparkline-weather") .DataSource(ds => ds .Read(r => r.Url("/Index?handler=Read").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 the Read operation that returns the dataset.C#public static List<Weather> items; public void OnGet() { var random = new Random(); if (items == null) { // Populate the "items" collection with data. items = new List<Weather>(); Enumerable.Range(0, 30).ToList().ForEach(i => items.Add(new Weather { Id = i, Rain = random.Next(0, 10), TMax = random.Next(2, 11), Wind = random.Next(8, 30) })); } } public JsonResult OnPostRead() { return new JsonResult(items); }