Ajax Binding
When configured for Ajax binding, the ListView for ASP.NET MVC makes Ajax requests when doing paging.
Getting Started
To configure the ListView for ASP.NET MVC for Ajax binding:
-
Add a new action method which returns data to populate the ListView. Note that the action method has a parameter of type
Kendo.Mvc.UI.DataSourceRequest
. It will contain the current ListView request information. Decorate that parameter with theKendo.Mvc.UI.DataSourceRequestAttribute
. This attribute is responsible for populating theDataSourceRequest
object.public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) { }
-
Use the
ToDataSourceResult
extension method to convert yourIQueryable
orIEnumerable
to aKendo.Mvc.UI.DataSourceResult
object. This extension method will page, filter, sort, or group your data using the information provided by theDataSourceRequest
object. To use theToDataSourceResult
extension method, import theKendo.Mvc.Extensions
namespace.public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) { // The result can be any IEnumerable or IQueriable. var result = myService.Orders; var dsResult = result.ToDataSourceResult(request); }
-
Return the
DataSourceResult
as JSON.public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) { // Get orders from the service. var orders = myService.Orders; // Apply the requested sort, page, group, filtering with the ToDataSourceResult() extension method. var dsResult = orders.ToDataSourceResult(request); return Json(dsResult); }
-
In the view, configure the ListView to use the action method created in the previous steps. You may use the sample model and data from the following example. The
ToDataSourceResult
method uses theDataSourceRequest
parameter and Linq expressions to apply paging, sorting, filtering, and grouping. The JSON response of the action method will contain only a single page of data. The ListView will be bound to that data.@(Html.Kendo().ListView<ListViewCore.Models.OrderViewModel>() .Name("ListView") .TagName("div") .ClientTemplateId("template") .DataSource(dataSource => dataSource .Ajax() .PageSize(6) .Read(read => read.Action("Orders_Read", "ListView")) ) .Pageable() )
<script type="text/x-kendo-tmpl" id="template"> <div class="order"> <h3>#= OrderID #</h3> <dl> <dt>Ship Name:</dt> <dd>#= ShipName #</dd> </dl> <dl> <dt>Ship City:</dt> <dd>#= ShipCity #</dd> </dl> <dl> <dt>Freight:</dt> <dd>#= kendo.toString(Freight, "n2")#</dd> </dl> <dl> <dt>Order Date:</dt> <dd>#= kendo.toString(OrderDate, "D")#</dd> </dl> </div> </script> <style> .order { float: left; width: 30%; height: 300px; position: relative; margin-left: 30px; text-align:center; } </style>
public class OrderViewModel { public int OrderID { get; set; } public decimal? Freight { get; set; } public DateTime? OrderDate { get; set; } public string ShipCity { get; set; } public string ShipName { get; set; } }
public class ListViewController : Controller { public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) { // The result can be any IEnumerable or IQueriable. // This is just an example that you can copy and paste. var result = Enumerable.Range(0, 50).Select(i => new OrderViewModel { OrderID = i, Freight = i * 10, OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7), ShipName = "ShipName " + i, ShipCity = "ShipCity " + i }); var dsResult = result.ToDataSourceResult(request); return Json(dsResult); } }
Adding More Parameters
To pass additional parameters to the action method:
- Use the
Data
setting. -
Provide the name of a JavaScript function which will return an object containing the additional data.
The following example demonstrates the action method.
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request, string firstName, string lastName) { // The implementation is omitted for brevity. }
The following example demonstrates how to send additional data.
@(Html.Kendo().ListView<ListViewCore.Models.OrderViewModel>() .Name("ListView") .TagName("div") .ClientTemplateId("template") .DataSource(dataSource => dataSource .Ajax() .PageSize(6) .Read(read => read.Action("Orders_Read", "ListView").Data("additionalData")) ) .Pageable() ) <script> function additionalData() { return { firstName: "John", lastName: "Doe" }; } </script>
Processing Client Data
By default, the Telerik UI ListView for ASP.NET MVC requests data from the server every time the user changes the page, filters the ListView, sorts, or groups. To change this behavior, disable ServerOperation
.
.DataSource(dataSource => dataSource
.ServerOperation(false) // All data will be requested at once and data operations will be applied client-side.
.Read(read => read.Action("Orders_Read", "ListView"))
)