# ListBox in Razor Pages

This article describes how to seamlessly integrate and configure the Telerik UI ListBox 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/editors/listbox/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

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 ListBox 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().ListBox()
       .Name("optional")
       .DataTextField("Text")
       .DataValueField("Value")
       .Toolbar(toolbar =>
       {
           toolbar.Position(ListBoxToolbarPosition.Right);
           toolbar.Tools(tools => tools
               .MoveUp()
               .MoveDown()
               .TransferTo()
               .TransferFrom()
               .TransferAllTo()
               .TransferAllFrom()
               .Remove()
           );
       })
       .DataSource(ds => ds
           .Read(r => r.Url(Url.Page("Index", "ReadOptional")).Data("forgeryToken"))
       )
       .ConnectWith("selected")
   )

   @(Html.Kendo().ListBox()
       .Name("selected")
       .DataTextField("Text")
       .DataValueField("Value")
       .Selectable(ListBoxSelectable.Multiple)
   )
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       @{
           var tools = new string[] {
               "moveUp",
               "moveDown",
               "transferTo",
               "transferFrom",
               "transferAllTo",
               "transferAllFrom",
               "remove"
           };
       }

       <kendo-listbox name="optional" datatextfield="Text" datavaluefield="Value" connect-with="selected">
           <datasource>
               <transport>
                   <read url="@Url.Page("Index", "ReadOptional")" data="forgeryToken"/>
               </transport>
           </datasource>
           <toolbar position="ListBoxToolbarPosition.Right" tools="tools"/>
       </kendo-listbox>

       <kendo-listbox name="selected" datatextfield="Text" datavaluefield="Value" selectable="ListBoxSelectable.Multiple" />
   ```

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.

   ```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 IList<SelectListItem> items;

       public void OnGet()
       {
           if (items == null)
           {
               items = new List<SelectListItem>();
               Enumerable.Range(1, 5).ToList().ForEach(f => items.Add(new SelectListItem
               {
                   Value = "" + f,
                   Text = "Text " + f
               }));
           }
       }

       public JsonResult OnPostReadOptional()()
       {
           return new JsonResult(items);
       }
   }
   ```

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

## Binding to a PageModel Property

To bind the ListBox 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 ListBox.

   ```C#
       public class ListBoxPageModel : PageModel
       {
           [BindProperty]
           public IList<SelectListItem> items { get; set; }

           public void OnGet()
           {
               if (items == null)
               {
                   items = new List<SelectListItem>();
                   Enumerable.Range(1, 5).ToList()
                       .ForEach(f => items.Add(new SelectListItem
                       {
                           Value = "" + f,
                           Text = "Text " + f
                       })
                   );
               }
           }
       }
   ```

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

   ```Razor
     @model ListBoxPageModel
   ```

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

   ```Razor HtmlHelper
       @page
       @model ListBoxPageModel

       @(Html.Kendo().ListBox(Model.items)
           .Name("optional")
           .DataTextField("Text")
           .DataValueField("Value")
           .Toolbar(toolbar =>
           {
               toolbar.Position(ListBoxToolbarPosition.Right);
               toolbar.Tools(tools => tools
                   .MoveUp()
                   .MoveDown()
                   .TransferTo()
                   .TransferFrom()
                   .TransferAllTo()
                   .TransferAllFrom()
                   .Remove()
               );
           })
           .ConnectWith("selected")
       )

       @(Html.Kendo().ListBox()
           .Name("selected")
           .DataTextField("Text")
           .DataValueField("Value")
           .Selectable(ListBoxSelectable.Multiple)
           .BindTo(new List<SelectListItem>())
       )
   ```

   ```Razor TagHelper
       @page
       @model ListBoxPageModel

       @{
           var tools = new string[] {
               "moveUp",
               "moveDown",
               "transferTo",
               "transferFrom",
               "transferAllTo",
               "transferAllFrom",
               "remove"
           };

           var selectedItems = new List<SelectListItem>();
       }

       <kendo-listbox name="optional" datatextfield="Text" datavaluefield="Value" connect-with="selected" bind-to="@Model.items">
           <toolbar position="ListBoxToolbarPosition.Right" tools="tools"/>
       </kendo-listbox>

       <kendo-listbox name="selected" datatextfield="Text" datavaluefield="Value" selectable="ListBoxSelectable.Multiple" bind-to="selectedItems"/>
   ```

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