New to Telerik UI for ASP.NET CoreStart a free 30-day trial

ListBox 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 design, the two files have the same name).

You can seamlessly integrate the Telerik UI ListBox for ASP.NET Core in Razor Pages applications.

This article describes how to configure the ListBox component in a Razor Pages scenario.

For the complete project, refer to the ListBox in Razor Pages example.

Getting Started

The DataSource 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.

    HtmlHelper_Index.cshtml
        @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("/Index?handler=ReadOptional").Data("forgeryToken"))
        )
        .ConnectWith("selected")
    )
    
    @(Html.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.

    Razor
        <script>
            function forgeryToken() {
                return kendo.antiForgeryTokens();
            }
        </script>

    Additional parameters can also be supplied.

    Razor
        <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.

    Index.cshtml.cs
        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);
        }

Binding the ListBox 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.

    Razor
        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)).

    HtmlHelper_Index.cshtml
        @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>())
        )

See Also