# DropDownList in Razor Pages

This article describes how to seamlessly integrate and configure the Telerik UI DropDownList 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/dropdownlist/binding/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 DropDownList to a dataset 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_Index.cshtml
   @(Html.Kendo().DropDownList()
       .Name("ordersDDL")
       .DataTextField("ShipName")
       .DataValueField("OrderID")
       .DataSource(source =>
       {
           source.Read(read => read
               .Url(Url.Page("Index", "Read")).Data("forgeryToken"));
       })
   )
   ```

   ```Razor TagHelper_Index.cshtml
   <kendo-dropdownlist name="ordersDDL"
       datatextfield="ShipName"
       datavaluefield="OrderID">
       <datasource>
           <transport>
               <read url="@Url.Page("Index", "Read")" data="forgeryToken"/>
           </transport>
       </datasource>
   </kendo-dropdownlist>
   ```

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. For example, when the [server filtering](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/editors/dropdownlist/filtering#server-filtering) of the DropDownList is enabled, send the filter value along with the antiforgery token to the server using the JavaScript handler specified in the `Data()` option.

   ```Razor HtmlHelper
       @page
       @model IndexModel
       <div>
           @(Html.Kendo().DropDownList()
               .Name("ordersDDL")
               .DataTextField("ShipName")
               .DataValueField("OrderID")
               .AutoBind(false)
               .Filter(FilterType.Contains)
               .MinLength(3)
               .DataSource(source =>
               {
                   source.Read(read => read
                       .Url(Url.Page("Index", "Read")).Data("dataFunction"))
                       .ServerFiltering(true);
               })
           )
       </div>
   ```

   ```Razor TagHelper
       @page
       @model IndexModel
       @addTagHelper *, Kendo.Mvc

       <div>
           <kendo-dropdownlist name="ordersDDL" auto-bind="false"
               datatextfield="ShipName"
               datavaluefield="OrderID"
               min-length="3"
               filter="FilterType.Contains">
               <datasource type="DataSourceTagHelperType.Custom" server-filtering="true">
                   <transport>
                       <read url="@Url.Page("Index", "Read")" data="dataFunction"/>
                   </transport>
               </datasource>
           </kendo-dropdownlist>
       </div>
   ```

   ```JS
       <script>
           function dataFunction(e) {
               var filterValue = '';
               if (e.filter.filters[0]) {
                   filterValue = e.filter.filters[0].value;
               }

               return {
                   __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken,
                   filterValue: filterValue
               };
           }
       </script>
   ```

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

   ```C# PageModel
       public class IndexModel : PageModel
       {
           public JsonResult OnGetRead()
           {
               var dropdownListData = new List<OrderViewModel>();
               // Populate the collection with the DropDownList data.
               return new JsonResult(dropdownListData);
           }
       }
   ```

   ```C# Model
   public class OrderViewModel
   {
       public int OrderID { get; set; }

       public string ShipName { get; set; }
   }
   ```

   When the server filtering is enabled, intercept the filter value sent through the `dataFunction` handler in the Read method and filter the data on the server before returning it to the DropDownList.

   ```C# PageModel
       public class IndexModel : PageModel
       {
           public JsonResult OnGetRead(string filterValue)
           {
               var dropdownListData = new List<OrderViewModel>();
               // Populate the collection with the DropDownList data.

               if (filterValue != null)
               {
                   var filteredData = dropdownListData.Where(p => p.ShipName.Contains(filterValue));
                   return new JsonResult(filteredData);
               }
               return new JsonResult(dropdownListData);
           }
       }
   ```

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

## Binding to a PageModel Property

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

1. Add a property to the `PageModel` that must bind to the DropDownList.

   ```C# PageModel
   public class IndexModel : PageModel
   {
       [BindProperty]
       public int OrderID { get; set; }

       public void OnGet()
       {
           OrderID = 2; // Assign a value to the "OrderID" property, if needed.
       }

       public JsonResult OnGetRead(string filterValue)
       {
           var dropdownListData = new List<OrderViewModel>();
           // Populate the collection with the DropDownList data.
           return new JsonResult(dropdownListData);
       }
   }
   ```

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

   ```Razor
       @page
       @model IndexModel
   ```

3. Bind the DropDownList to the property using the `DropDownListFor()` configuration.

   ```Razor HtmlHelper
       @page
       @model IndexModel

       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()
       
       @(Html.Kendo().DropDownListFor(m => m.OrderID)  
           .DataTextField("ShipName")
           .DataValueField("OrderID")
           .DataSource(source =>
           {
               source.Read(read => read
                   .Url(Url.Page("Index", "Read")).Data("forgeryToken"));
           })
       )
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()
       @addTagHelper *, Kendo.Mvc

       <kendo-dropdownlist for="OrderID"
           datatextfield="ShipName" 
           datavaluefield="OrderID">
           <datasource>
               <transport>
                   <read url="@Url.Page("Index", "Read")" data="forgeryToken"/>
               </transport>
           </datasource>
       </kendo-dropdownlist>
   ```

   ```JS
       <script>
           function forgeryToken(e) {
               return kendo.antiForgeryTokens();
           }
       </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 DropDownList](https://docs.telerik.com/kendo-ui/api/ui/dropdownlist)
* [Server-Side HtmlHelper API of the DropDownList](https://www.telerik.com/aspnet-core-ui/documentation/api/dropdownlist)
* [Server-Side TagHelper API of the DropDownList](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/dropdownlist)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
