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

MultiSelect 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 MultiSelect for ASP.NET Core in Razor Pages applications.

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

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

In order to set up the MultiSelect component bindings, you need to configure the Read method of its DataSource instance. The URL in this method should refer the name of the method in the PageModel. In this method, you can also pass additional parameters, such as filter string and antiforgery token (see dataFunction).

cshtml
    @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
	@Html.AntiForgeryToken()
	
	<h1>MultiSelect Index</h1>
	
	@(Html.Kendo().MultiSelect()
        .Name("customers")
        .DataTextField("Name")
        .DataValueField("CustomerId")
        .Filter(FilterType.Contains)
        .ItemTemplate("<span>  #: data.CustomerId # </span> <h4> #: data.Name # <h4>")
        .DataSource(ds => ds
            .Custom()
            .Transport(transport => transport
                .Read(r => r
                    .Url("/MultiSelect/MultiSelectIndex?handler=Read").Data("forgeryToken")
                )
            )
            .ServerFiltering(false)
        )
    )

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

Binding the MultiSelect to a PageModel Property

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

  1. Add a property to the PageModel that must bind to the MultiSelect.

    Index.cshtml.cs
        public class IndexModel : PageModel
        {
            public int[] Orders { get; set; }
    
    
            public void OnGet()
            {
                Orders = new int[] { 2, 3 }; // Assign value to the "Orders" property, if needed.
            }
    
            public JsonResult OnGetRead()
            {
                var multiSelectData = new List<OrderViewModel>();
                // Populate the collection with the MultiSelect data.
                return new JsonResult(multiSelectData);
            }
        }
  2. Declare the PageModel at the top of the page.

    C#
        @page
        @model IndexModel
  3. Bind the MultiSelect to the property using the MultiSelectFor() configuration.

    HtmlHelper_Index.cshtml
        @page
        @model IndexModel
    
        @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
        @Html.AntiForgeryToken()
        
        @(Html.Kendo().MultiSelectFor(m => m.Orders)  
            .DataTextField("ShipName")
            .DataValueField("OrderID")
            .DataSource(source =>
            {
                source.Read(read => read
                    .Url("/Index?handler=Read").Data("forgeryToken"));
            })
        )
    
        <script>
            function forgeryToken(e) {
                return kendo.antiForgeryTokens();
            }
        </script>

See Also