# ScrollView in Razor Pages

This article describes how to seamlessly integrate and configure the Telerik UI ScrollView for ASP.NET Core in Razor Pages applications.

## Referencing Handler Methods 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 convention, the two files have the same name).

The `cshtml.cs` file, known as the `PageModel`, contains handler methods that respond to HTTP requests. These methods are prefixed with `On` followed by the HTTP verb (for example, `OnGet`, `OnPost`, `OnPostRead`, `OnPostCreate`).

Handler methods declared in a `PageModel` can be referenced from any Razor Page using one of the following URL patterns:

* Using `Url.Page()`

  ```C#
  Url.Page("PageName", "HandlerName")
  // OR
  Url.Page("/FolderName/PageName", "HandlerName")
  ```

  For example, `Url.Page("Index", "Read")` references the `OnPostRead` or `OnGetRead` handler method in the `Index.cshtml.cs` file.

* Using a query string

  ```C#
  Url("/PathToPage?handler=HandlerName")
  ```

  For example, `Url("/Index?handler=Read")` references the `OnPostRead` or `OnGetRead` handler method in the `Index` page.

For more information on Razor Pages architecture and concepts, refer to [the official Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/).

## Binding to Remote Data

The [DataSource](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/datasource/overview) 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:

1. Specify the Read request URL in the `DataSource` configuration. The URL must refer to the method name in the `PageModel`.

   ```Razor HtmlHelper
       @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>
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       <div id="example" style="margin:auto; width:60%">
           <kendo-scrollview name="scrollView"
               content-height="100%"
               template-id="scrollview-template"
               style=height:500px; width:890px; max-width: 100%;>
               <datasource custom-type="aspnetmvc-ajax" page-size="3" server-paging="true">
                   <transport>
                       <read url="/Index?handler=ReadOptional" data="forgeryToken"/>
                   </transport>
               </datasource>
           </kendo-scrollview>
       </div>
   ```

   ```JS scrollview-template
       <script id="scrollview-template" type="text/x-kendo-template">
           <div class="img-wrapper">
               # for (var i = 0; i < data.length; i++) { #
               <div>
                   <div>
                       <img style="width: 140px; height: 140px;" src="@Url.Content("~/Images/ScrollViewImages/")#:data[i].ImageUrl# " />
                   </div>
                   <p>#= data[i].ProductName #</p>
               </div>
               # } #
           </div>
       </script>
   ```

2. Add an `AntiForgeryToken` at the top of the page.

   ```C#
       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()
   ```

3. 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>
   ```

4. Within the `cshtml.cs` file, add a handler method for the Read operation that returns the dataset.

   ```C# Index.cshtml.cs
   public class IndexModel : PageModel
   {
       public 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));
       }
   }
   ```

   ```C# Model
       public class Product
       {
           public int ProductID { get; set; }
           public string ProductName { get; set; }
           public string ImageUrl { get; set; }
       }
   ```

For the complete project, refer to the [ScrollView in Razor Pages example](https://github.com/telerik/ui-for-aspnet-core-examples/blob/master/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/ScrollView/ScrollViewBinding.cshtml).

## See Also

* [Using Telerik UI for ASP.NET Core in Razor Pages](https://docs.telerik.com/aspnet-core/getting-started/razor-pages#using-telerik-ui-for-aspnet-core-in-razor-pages)
* [Client-Side API of the ScrollView](https://docs.telerik.com/kendo-ui/api/ui/scrollview)
* [Server-Side HtmlHelper API of the ScrollView](https://www.telerik.com/aspnet-core-ui/documentation/api/scrollview)
* [Server-Side TagHelper API of the ScrollView](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/scrollview)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
