ScrollView 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 ScrollView for ASP.NET Core in Razor Pages applications.
This article describes how to configure the ScrollView component in a Razor Pages scenario.
For the complete project, refer to the ScrollView in Razor Pages example.
Getting Started
The DataSource component offers the most versatile data binding approach. To connect the ScrollView 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
DataSource
configuration. The URL must refer to the method name in thePageModel
.HtmlHelper_Index.cshtml@page @model IndexModel <div id="example" style="margin:auto; width:60%"> @(Html.Kendo().ScrollView() .Name("scrollView") .ContentHeight("100%") .TemplateId("scrollview-template") .DataSource(source => source .Ajax() .Read(r => r.Url("/Index?handler=ReadOptional").Data("forgeryToken")) .PageSize(3) ) .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" }) ) </div>
-
Add an
AntiForgeryToken
at the top of the page.C#@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken()
-
Send the
AntiForgeryToken
ith the Read request.JS<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>
Additional parameters can also be supplied.
JS<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.Index.cshtml.cspublic static List<Product> ScrollViewItems { get; set; } public void OnGet() { if (ScrollViewItems == null) { // Populate the "ScrollViewItems" collection with data. ScrollViewItems = new List<Product>(); ScrollViewItems.Add(new Product { ImageUrl = "image1.jpg", ProductName = "Chai" }); ScrollViewItems.Add(new Product { ImageUrl = "image2.jpg", ProductName = "Chang" }); ScrollViewItems.Add(new Product { ImageUrl = "image3.jpg", ProductName = "Aniseed Syrup" }); ScrollViewItems.Add(new Product { ImageUrl = "image4.jpg", ProductName = "Ikura" }); ScrollViewItems.Add(new Product { ImageUrl = "image5.jpg", ProductName = "Tofu" }); ScrollViewItems.Add(new Product { ImageUrl = "image6.jpg", ProductName = "Konbu" }); ScrollViewItems.Add(new Product { ImageUrl = "image7.jpg", ProductName = "Pavlova" }); ScrollViewItems.Add(new Product { ImageUrl = "image8.jpg", ProductName = "Cloud" }); ScrollViewItems.Add(new Product { ImageUrl = "image9.jpg", ProductName = "Sun" }); } } public JsonResult OnPostReadOptional([DataSourceRequest] DataSourceRequest request) { return new JsonResult(ScrollViewItems.ToDataSourceResult(request)); }