# ListView in Razor Pages

This article describes how to seamlessly integrate and configure the Telerik UI ListView 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/data-management/listview/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 configure the CRUD operations of the ListView `DataSource` within a Razor Pages application, follow the next steps:

1. Specify the `Read`, `Create`, `Update`, and `Destroy` options of the `DataSource` configuration. The URL in each of these options must refer to the method name in the `PageModel`

   ```Razor HtmlHelper
       @page
       @model IndexModel

       @(Html.Kendo().ListView<OrderViewModel>()
           .Name("listview")
           .Pageable()
           .Editable()
           .TagName("div")
           .ClientTemplateId("template")
           .DataSource(ds => ds
               .Ajax()
               .Model(model => model.Id(p => p.OrderID))
               .PageSize(18)
               .Create(create => create.Url(Url.Page("Index", "Create")).Data("forgeryToken"))
               .Read(read => read.Url(Url.Page("Index", "Read")).Data("forgeryToken"))
               .Update(update => update.Url(Url.Page("Index", "Update")).Data("forgeryToken"))
               .Destroy(destroy => destroy.Url(Url.Page("Index", "Destroy")).Data("forgeryToken"))
           )
       )
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       <kendo-listview name="listview" tag-name="div" template-id="template" edit-template-id="editTemplate">
           <datasource type="DataSourceTagHelperType.Ajax" page-size="18">
               <schema>
                   <model id="OrderID"/>
               </schema>
               <transport>
                   <create url="@Url.Page("Index", "Create")" data="forgeryToken" />
                   <read url="@Url.Page("Index", "Read")" data="forgeryToken" />
                   <update url="@Url.Page("Index", "Update")" data="forgeryToken" />
                   <destroy url="@Url.Page("Index", "Destroy")" data="forgeryToken"/>
               </transport>
           </datasource>
           <pageable enabled="true"/>
       </kendo-listview>

       <script type="text/x-kendo-template" id="editTemplate">
           <div class="order-view k-widget">
               <dl>
                   <dt>Freight</dt>
                   <dd>
                       <input id="Freight" type="text" data-bind="value:Freight" data-role="numerictextbox" data-type="number" name="Freight" required="required" min="1" validationMessage="required" />
                       <span data-for="Freight" class="k-invalid-msg"></span>
                   </dd>
                   <dt>Ship City</dt>
                   <dd>
                       <span class="k-textbox k-input">
                           <input id="ShipCity" type="text" class="k-input-inner" data-bind="value:ShipCity" name="ShipCity" required="required" validationMessage="required" />
                       </span>
                       <span data-for="ShipCity" class="k-invalid-msg"></span>
                   </dd>
                   <dt>Ship Name</dt>
                   <dd>
                       <span class="k-textbox k-input">
                           <input id="ShipName" type="text" class="k-input-inner" data-bind="value:ShipName" name="ShipName" required="required" validationMessage="required" />
                       </span>
                       <span data-for="ShipName" class="k-invalid-msg"></span>
                   </dd>
               </dl>
               <div class="edit-buttons">
                   <a role="button" class="k-button k-update-button" href="\\#">#= kendo.ui.icon({ icon: 'check' }) #</a>
                   <a role="button" class="k-button k-cancel-button" href="\\#">#= kendo.ui.icon({ icon: 'cancel' }) #</a>
               </div>
           </div>
       </script>
   ```

   ```JS Template
       <script type="text/x-kendo-tmpl" id="template">
           <div class="order-view k-widget">
               <dl>
                   <dt>Freight</dt>
                   <dd>#:Freight#</dd>
                   <dt>Ship City</dt>
                   <dd>#:ShipCity#</dd>
                   <dt>Ship Name</dt>
                   <dd>#:ShipName#</dd>
               </dl>
               <div class="edit-buttons">
                   <a role="button" class="k-button k-edit-button" href="\\#">#= kendo.ui.icon({ icon: 'pencil' }) #</a>
                   <a role="button" class="k-button k-delete-button" href="\\#">#= kendo.ui.icon({ icon: 'x' }) #</a>
               </div>
           </div>
       </script>
   ```

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 CRUD requests.

   ```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 each data operation.

   ```C# Index.cshtml.cs
   public class IndexModel : PageModel
   {
       public static IList<OrderViewModel> orders;

       public void OnGet()
       {
           if (orders == null)
           {
               // Populate the "orders" collection with data.
               orders = new List<OrderViewModel>();
               Enumerable.Range(1, 50).ToList().ForEach(i => orders.Add(new OrderViewModel
               {
                   OrderID = i,
                   Freight = i * 10,
                   ShipName = "ShipName " + i,
                   ShipCity = "ShipCity " + i
               }));
           }
       }

       public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request)
       {
           return new JsonResult(orders.ToDataSourceResult(request));
       }

       public JsonResult OnPostCreate([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
       {
           order.OrderID = orders.Count + 1;
           orders.Add(order);

           return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState));
       }

       public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
       {
           orders.Where(x => x.OrderID == order.OrderID).Select(x => order);

           return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState));
       }

       public JsonResult OnPostDestroy([DataSourceRequest] DataSourceRequest request, OrderViewModel order)
       {
           orders.Remove(orders.FirstOrDefault(x => x.OrderID == order.OrderID));

           return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState));
       }
   }
   ```

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

## Binding to a PageModel Property

To bind the ListView to a property from the `PageModel`, follow the next steps:

1. Add a property to the `PageModel` that holds the data collection that must be loaded in the ListView.

   ```C#
       public class ListViewPageModel : PageModel
       {
           [BindProperty]
           public IList<OrderViewModel> orders { get; set; }

           public void OnGet()
           {
               orders = new List<OrderViewModel>();
               // Populate the collection with data.
               Enumerable.Range(1, 50).ToList().ForEach(i => orders.Add(new OrderViewModel
               {
                   OrderID = i + 1,
                   Freight = i * 10,
                   ShipName = "ShipName " + i,
                   ShipCity = "ShipCity " + i
               }));
           }
       }
   ```

2. Declare the `PageModel` at the top of the page.

   ```Razor
       @model ListViewPageModel
   ```

3. Bind the ListView to the collection property and disable the server data operations (`ServerOperations(false)`).

   ```Razor HtmlHelper
       @page
       @model ListViewPageModel

       @(Html.Kendo().ListView<OrderViewModel>(Model.orders)
           .Name("listview")
           .Pageable()
           .TagName("div")
           .ClientTemplateId("template")
           .DataSource(ds => ds
               .Ajax()
               .PageSize(18)
               .ServerOperation(false)
           )
       )
   ```

   ```Razor TagHelper
       @page
       @model ListViewPageModel

       <kendo-listview name="listview" tag-name="div" template-id="template" bind-to="@Model.orders">
           <pageable enabled="true"/>
           <datasource type="DataSourceTagHelperType.Ajax" page-size="18" server-operation="false" />
       </kendo-listview>
   ```

   ```JS Template
       <script type="text/x-kendo-tmpl" id="template">
           <div class="order-view k-widget">
               <dl>
                   <dt>Freight</dt>
                   <dd>#:Freight#</dd>
                   <dt>Ship City</dt>
                   <dd>#:ShipCity#</dd>
                   <dt>Ship Name</dt>
                   <dd>#:ShipName#</dd>
               </dl>
               <div class="edit-buttons">
                   <a role="button" class="k-button k-edit-button" href="\\#">#= kendo.ui.icon({ icon: 'pencil' }) #</a>
                   <a role="button" class="k-button k-delete-button" href="\\#">#= kendo.ui.icon({ icon: 'x' }) #</a>
               </div>
           </div>
       </script>
   ```

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