MultiColumnComboBox 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 MultiColumnComboBox for ASP.NET Core in Razor Pages applications.
This article describes how to configure the MultiColumnComboBox component in a Razor Pages scenario.
For the complete project, refer to the MultiColumnComboBox in Razor Pages example.
In order to set up the MultiColumnComboBox 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
).
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@Html.AntiForgeryToken()
<h1>MultiColumnComboBox Index</h1>
@(Html.Kendo().MultiColumnComboBox()
.Name("products")
.DataTextField("ShipName")
.DataValueField("ShipCity")
.Filter(FilterType.Contains)
.Columns(columns =>
{
columns.Add().Field("ShipName").Title("Ship Name").Width("200px");
columns.Add().Field("ShipCity").Title("Ship City").Width("200px");
columns.Add().Field("Freight").Title("Freight").Width("200px");
})
.DataSource(ds => ds
.Custom()
.Transport(transport => transport
.Read(r => r
.Url("/MultiColumnComboBox/MultiColumnComboBoxIndex?handler=Read").Data("dataFunction")
))
.ServerFiltering(true)
)
)
<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>
Binding the MultiColumnComboBox to a PageModel Property
To bind the MultiColumnComboBox to a property from the PageModel
, follow the next steps:
-
Add a property to the
PageModel
that must bind to the MultiColumnComboBox.Index.cshtml.cspublic 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() { var comboBoxData = new List<OrderViewModel>(); // Populate the collection with the ComboBox data. return new JsonResult(comboBoxData); } }
-
Declare the
PageModel
at the top of the page.C#@page @model IndexModel
-
Bind the MultiColumnComboBox to the property using the
MultiColumnComboBoxFor()
configuration.HtmlHelper_Index.cshtml@page @model IndexModel @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() @(Html.Kendo().MultiColumnComboBoxFor(m => m.OrderID) .DataTextField("ShipName") .DataValueField("OrderID") .DataSource(source => { source.Read(read => read .Url("/Index?handler=Read").Data("forgeryToken")); }) )