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
DataSource
configuration. The URL must refer to the method name in thePageModel
.@page @model IndexModel @(Html.Kendo().Sparkline() .Name("sparkline-weather") .DataSource(ds => ds .Read(r => r.Url("/Index?handler=Read").Data("forgeryToken")) ) ... )
@page @model IndexModel <kendo-sparkline name="sparkline-weather"> <datasource type="DataSourceTagHelperType.Ajax"> <transport> <read type="post" url="/Index?handler=Read" data="forgeryToken" /> </transport> </datasource> <!--Other configuration--> </kendo-sparkline>
-
Add an
AntiForgeryToken
at the top of the page.@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken()
-
Send the
AntiForgeryToken
with the Read request.<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>
Additional parameters can also be supplied.
<script> function forgeryToken() { return { __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken, additionalParameter: "test" } } </script>
-
Within the
cshtml.cs
file, add a handler method for the Read operation that returns the dataset.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); }
public class Weather { public int Id { get; set; } public double Rain { get; set; } public double TMax { get; set; } public double Wind { get; set; } }