Grid in Razor Pages
This article describes how to seamlessly integrate and configure the Telerik UI Grid 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 Grid 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 thePageModel.Razor@page @model IndexModel @using Kendo.Mvc.UI @(Html.Kendo().Grid<OrderViewModel>() .Name("grid") .Editable() .Scrollable() .Pageable() .ToolBar(t => t.Create()) .Columns(columns => { columns.Bound(column => column.Freight); columns.Bound(column => column.ShipName); columns.Bound(column => column.ShipCity); columns.Command(column => { column.Edit(); column.Destroy(); }).Width(230); }) .HtmlAttributes(new { style = "height:430px;" }) .DataSource(ds => ds .Ajax() .Read(r => r.Url(Url.Page("Index", "Read")).Data("forgeryToken")) .Update(u => u.Url(Url.Page("Index", "Update")).Data("forgeryToken")) .Create(c => c.Url(Url.Page("Index", "Create")).Data("forgeryToken")) .Destroy(d => d.Url(Url.Page("Index", "Destroy")).Data("forgeryToken")) .Model(m => m.Id(id => id.OrderID)) .PageSize(10) ) ) -
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 Grid in Razor Pages example.
Binding to a PageModel Property
To bind the Grid 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 Grid.C#public class IndexModel : 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 IndexModel -
Bind the Grid to the collection property and disable the server data operations (
ServerOperations(false)).Razor@page @model IndexModel @using Kendo.Mvc.UI @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() @(Html.Kendo().Grid<OrderViewModel>(Model.orders) .Name("grid") .Scrollable() .Pageable() .Columns(columns => { columns.Bound(column => column.Freight); columns.Bound(column => column.ShipName); columns.Bound(column => column.ShipCity); }) .HtmlAttributes(new { style = "height:430px;" }) .DataSource(ds => ds .Ajax() .PageSize(20) .ServerOperation(false) ) )