# ASP.NET MVC DataSource Overview

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.

* [Demo page for the DataSource HtmlHelper](https://demos.telerik.com/aspnet-mvc/datasource/index)

## 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](https://docs.telerik.com/kendo-ui/api/data/datasource).

```Razor HtmlHelper
    @(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>  
```

```C# HomeController

    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 the `ToDataSourceResult` 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 of `ToDataSourceResult` by leveraging the `async` and `await` 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:

```C#
    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](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/sort#sort) the data based on an object, set [the data field, by which the data items are sorted,](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/sort#sortfield) to a property of that object.
> * To [group](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/group) the data by an object, set [the group by data item field](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/group#groupfield) to a property of that object.
> * To [filter](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/filter) the data based on an object, set [the data item field, to which the filter operator is applied,](https://docs.telerik.com/kendo-ui/api/data/datasource/configuration/filter#filterfield) to a property of that object.

The configuration accepts the definition for all CRUD operations and facilitates the data sorting, filtering, and grouping.

```Razor HtmlHelper
    @(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](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/faq#how-can-i-prevent-ajax-response-caching)

## 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](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base/datasource-model-mapping).

```C#
    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](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/model)            | Many scenarios require you to configure the Model of the DataSource.                                   |
| [Aggregates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/aggregates)  | You can easily calculate the aggregates of the data set like Min, Max, Average, etc.                   |
| [Filtering](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/filter)       | The built-in filtering enables you to search for a subset of data among the items.                     |
| [Sorting](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/sort)           | The DataSource supports ascending and descending sorting.                                              |
| [Grouping](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/group)         | You can group the returned data based on a common criteria.                                            |
| [Headers](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/headers)        | You can set request headers by using the Headers configuration option of the DataSource.               |
| [DataSource Types](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/types) | You can choose the type of DataSource that best fits your needs.                                       |
| [CRUD Operations](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/crud)   | The DataSource supports easy set up of its CRUD operations and handles the server response on its own. |

## Next Steps

* [Getting Started with the DataSource](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/getting-started)
* [Basic Usage of the DataSource HtmlHelper for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/datasource/index)

## See Also

* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
* [Server-Side HtmlHelper API](https://www.telerik.com/aspnet-mvc/documentation/api/datasource)
