# Chart Wizard in Razor Pages

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

> You can use any of the available [data binding approaches](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/chartwizard/binding/overview#data-binding-approaches) to bind the component to data in a Razor Pages application.

## 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

To connect the Chart Wizard 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

       @(Html.Kendo().ChartWizard<Product>()
           .Name("chartwizard")
           .DataSource(dataSource => dataSource
               .Read(r => r.Url(Url.Page("Index", "Read")).Data("forgeryToken"))
           )
           .DataColumns(columns =>
           {
               columns.Add().Field(f => f.ProductName).Title("Product Name");
               columns.Add().Field(f => f.Quantity);
           })
       )
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       <kendo-chartwizard name="chartwizard">
           <datasource type="DataSourceTagHelperType.Ajax">
               <schema data="Data" total="Total" errors="Errors">
                   <model>
                       <fields>
                           <field name="ProductName" type="string"></field>
                           <field name="Quantity" type="number"></field>
                       </fields>
                   </model>
               </schema>
               <transport>
                   <read url="@Url.Page("Index", "Read")" data="forgeryToken"/>
               </transport>
           </datasource>
           <data-columns>
               <data-column field="ProductName" title="Product Name"/>
               <data-column field="Quantity" />
           </data-columns>
       </kendo-chartwizard>
   ```

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

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

3. Send the `AntiForgeryToken` with 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>
   ```

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

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

   ```C# Model
       public class Product
       {
           public int ProductID { get; set; }
           public string ProductName { get; set; }
           public int Quantity { get; set; }
           public decimal Price { get; set; }
           public decimal Tax { get; set; }
           public decimal Total { get; set; }
           public decimal TotalTax { get; set; }
       }
   ```

## 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 Chart Wizard](https://docs.telerik.com/kendo-ui/api/ui/chartwizard)
* [Server-Side HtmlHelper API of the Chart Wizard](https://www.telerik.com/aspnet-core-ui/documentation/api/chartwizard)
* [Server-Side TagHelper API of the Chart Wizard](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/chartwizard)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
