ASP.NET MVC DataSource Overview
The Data Source is part of Telerik UI for ASP.NET MVC, a
professional grade UI library with 110+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.
The Telerik UI DataSource HtmlHelper for ASP.NET MVC is a server-side wrapper for the Kendo UI for jQuery DataSource.
The DataSource is an abstraction for using local data or remote data. In most cases, the DataSource definition is declared as part of the configurations for the Telerik UI helpers. The standalone DataSource component is suitable for scenarios that require a shared data source.
Initialize the DataSource
The following example demonstrates how to define the DataSource. You can access the DataSource instance by Name()
on the client and use the API methods and events of the Kendo UI for jQuery DataSource widget.
@(Html.Kendo().DataSource<OrderViewModel>()
.Name("myDataSource")
.Ajax(d=>d.Read(r => r.Action("ReadOrders", "Home")))
)
<script>
$(document).ready(function () {
myDataSource.read(); // A POST request will be sent to the HomeController "ReadOrders" action.
});
</script>
public JsonResult ReadOrders([DataSourceRequest]DataSourceRequest request)
{
// Orders can be IQueriable or IEnumerable.
return Json(orders.ToDataSourceResult(request));
}
- If your data is
IQueryable<T>
returned by a LINQ-enabled provider—Entity Framework, LINQ to SQL, Telerik OpenAccess, NHibernate or other—the LINQ expressions, created by theToDataSourceResult
method, are converted to SQL and executed by the database server.- Use the
ToDataSourceResult()
method to page, sort, filter, and group the collection that is passed to it. If this collection is already paged, the method returns an empty result.- As of the R1 2017 SP1 release, you can use the
ToDataSourceResultAsync
extension method to provide the asynchronous functionality ofToDataSourceResult
by leveraging theasync
andawait
features of the .NET Framework.- If impersonation is enabled, use the
ToDataSourceResultAsync
extension method with only one thread in your ASP.NET application. If you create a new thread, the impersonation in the newly created child thread decreases because, by default, all newly created child threads in ASP.NET run under the ASP.NET identity of the worker process. To change this behavior, explicitly impersonate the current identity within the code of the child thread.
To use DataSourceRequest
and ToDataSourceResult()
with the DataSource HtmlHelper, add the following namespaces with using
directives in the Controller:
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
Basic Configuration
You can declare the DataSource component configuration options by using the available methods—for example, you can define the page size, page, sort order, filter, group, aggregates, and the model.
- To sort the data based on an object, set the data field, by which the data items are sorted, to a property of that object.
- To group the data by an object, set the group by data item field to a property of that object.
- To filter the data based on an object, set the data item field, to which the filter operator is applied, to a property of that object.
The configuration accepts the definition for all CRUD operations and facilitates the data sorting, filtering, and grouping.
@(Html.Kendo().DataSource<OrderViewModel>()
.Name("myDataSource")
.Ajax(dataSource =>
{
dataSource
.Read(read => read.Action("ReadOrders", "Home"))
.Sort(sort => sort.Add(field => field.ShipCountry).Ascending())
.Filter(filter=>filter.Add(field=>field.ShipCountry).StartsWith("A"))
.Group(group=>group.Add(field=>field.OrderID))
.PageSize(20)
.ServerOperation(true)
.Model(model =>
{
model.Id(field => field.OrderID);
model.Field(field => field.OrderID).Editable(false);
model.Field(field => field.ShipCountry).DefaultValue("USA");
});
})
)
<script>
myDataSource.fetch();
</script>
Prevent Ajax Response Caching
To prevent Ajax response caching, refer to this section from the Frequently Asked Questions article
Model Mapping
In some cases, you may use view model objects instead of entities returned by Entity Framework. For example, you may want to avoid serializing all Entity Framework properties as JSON or prevent serialization exceptions caused by circular references.
To map to a ViewModel on the fly, pass a mapping lambda as a second parameter to the ToDataSourceResult()
extension method.
The naming of the model properties of the view model objects and entities returned by Entity Framework must match. If usage of different naming is desired, implement model mapping.
public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
{
using (var northwind = new NorthwindEntities())
{
IQueryable<Product> products = northwind.Products;
// Convert the Product entities to ProductViewModel instances.
DataSourceResult result = products.ToDataSourceResult(request, product => new ProductViewModel
{
ProductID = product.ProductID,
ProductName = product.ProductName,
UnitsInStock = product.UnitsInStock
});
return Json(result);
}
}
Functionality and Features
Feature | Description |
---|---|
Model | Many scenarios require you to configure the Model of the DataSource. |
Aggregates | You can easily calculate the aggregates of the data set like Min, Max, Average, etc. |
Filtering | The built-in filtering enables you to search for a subset of data among the items. |
Sorting | The DataSource supports ascending and descending sorting. |
Grouping | You can group the returned data based on a common criteria. |
Headers | You can set request headers by using the Headers configuration option of the DataSource. |
DataSource Types | You can choose the type of DataSource that best fits your needs. |
CRUD Operations | The DataSource supports easy set up of its CRUD operations and handles the server response on its own. |