ListView in Razor Pages
This article describes how to seamlessly integrate and configure the Telerik UI ListView for ASP.NET Core in Razor Pages applications.
You can use any of the available 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 theOnPostReadorOnGetReadhandler method in theIndex.cshtml.csfile. -
Using a query string
C#Url("/PathToPage?handler=HandlerName")For example,
Url("/Index?handler=Read")references theOnPostReadorOnGetReadhandler method in theIndexpage.
For more information on Razor Pages architecture and concepts, refer to the official Microsoft documentation.
Binding to Remote Data
To configure the CRUD operations of the ListView DataSource within a Razor Pages application, follow the next steps:
-
Specify the
Read,Create,Update, andDestroyoptions of theDataSourceconfiguration. The URL in each of these options must refer to the method name in thePageModelRazor@page @model IndexModel @(Html.Kendo().ListView<OrderViewModel>() .Name("listview") .Pageable() .Editable() .TagName("div") .ClientTemplateId("template") .DataSource(ds => ds .Ajax() .Model(model => model.Id(p => p.OrderID)) .PageSize(18) .Create(create => create.Url(Url.Page("Index", "Create")).Data("forgeryToken")) .Read(read => read.Url(Url.Page("Index", "Read")).Data("forgeryToken")) .Update(update => update.Url(Url.Page("Index", "Update")).Data("forgeryToken")) .Destroy(destroy => destroy.Url(Url.Page("Index", "Destroy")).Data("forgeryToken")) ) ) -
Add an
AntiForgeryTokenat the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith the CRUD requests.JavaScript<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>Additional parameters can also be supplied.
JavaScript<script> function forgeryToken() { return { __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken, additionalParameter: "test" } } </script> -
Within the
cshtml.csfile, add a handler method for each data operation.C#public class IndexModel : PageModel { public static IList<OrderViewModel> orders; public void OnGet() { if (orders == null) { // Populate the "orders" collection with data. orders = new List<OrderViewModel>(); Enumerable.Range(1, 50).ToList().ForEach(i => orders.Add(new OrderViewModel { OrderID = i, Freight = i * 10, ShipName = "ShipName " + i, ShipCity = "ShipCity " + i })); } } public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request) { return new JsonResult(orders.ToDataSourceResult(request)); } public JsonResult OnPostCreate([DataSourceRequest] DataSourceRequest request, OrderViewModel order) { order.OrderID = orders.Count + 1; orders.Add(order); return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, OrderViewModel order) { orders.Where(x => x.OrderID == order.OrderID).Select(x => order); return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState)); } public JsonResult OnPostDestroy([DataSourceRequest] DataSourceRequest request, OrderViewModel order) { orders.Remove(orders.FirstOrDefault(x => x.OrderID == order.OrderID)); return new JsonResult(new[] { order }.ToDataSourceResult(request, ModelState)); } }
For the complete project, refer to the ListView in Razor Pages example.
Binding to a PageModel Property
To bind the ListView to a property from the PageModel, follow the next steps:
-
Add a property to the
PageModelthat holds the data collection that must be loaded in the ListView.C#public class ListViewPageModel : PageModel { [BindProperty] public IList<OrderViewModel> orders { get; set; } public void OnGet() { orders = new List<OrderViewModel>(); // Populate the collection with data. Enumerable.Range(1, 50).ToList().ForEach(i => orders.Add(new OrderViewModel { OrderID = i + 1, Freight = i * 10, ShipName = "ShipName " + i, ShipCity = "ShipCity " + i })); } } -
Declare the
PageModelat the top of the page.Razor@model ListViewPageModel -
Bind the ListView to the collection property and disable the server data operations (
ServerOperations(false)).Razor@page @model ListViewPageModel @(Html.Kendo().ListView<OrderViewModel>(Model.orders) .Name("listview") .Pageable() .TagName("div") .ClientTemplateId("template") .DataSource(ds => ds .Ajax() .PageSize(18) .ServerOperation(false) ) )