# Server Detail Templates

The Telerik UI Grid for ASP.NET MVC enables you to set the detail template that is used during the server binding of the Grid.

For a runnable example, refer to the [demo on the server detail template in the Grid](https://demos.telerik.com/aspnet-mvc/grid/serverdetails).

To configure the server detail template:

1. [Define the server details](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/templates/server-detail-template#defining-the-server-details)
2. [Set the server hierarchy](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/templates/server-detail-template#setting-the-server-hierarchy)

## Defining the Server Details

1. Create a new ASP.NET MVC 5 application. If you have installed the [Telerik UI for ASP.NET MVC Visual Studio Extensions](https://www.telerik.com/aspnet-mvc/documentation/vs-integration/introduction), create a Telerik UI for ASP.NET MVC application. Name the application `KendoGridServerDetailTemplate`. If you decided not to use the Telerik UI for ASP.NET MVC Visual Studio Extensions, follow the steps from the [First Steps article](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps) to add Telerik UI for ASP.NET MVC to the application.

2. Add a new `Entity Framework Data Model`. Right-click the `~/Models` folder in the solution explorer and pick **Add** > **New Item**. Choose **Data** > **ADO.NET Entity Data Model** in the **Add New Item** dialog. Name the model `Northwind.edmx` and click **Next**. This starts the **Entity Data Model Wizard**.

   ![UI for ASP.NET MVC A new entity data model](https://www.telerik.com/aspnet-mvc/documentation/assets/1286540ae776a0b9803b365ff9a0c775/grid-entity-data-model.png)

3. Select **Generate from database** and click **Next**. Configure a connection to the **Northwind** database. Click **Next**.

   ![UI for ASP.NET MVC Choosing the connection](https://www.telerik.com/aspnet-mvc/documentation/assets/1b29022c17fddc909b4d7bd3de4fbdce/grid-binding-choose-data-connection.png)

4. Choose the **Products** table from **Which database objects do you want to include in your model?**. Leave all other options as they are set by default. Click **Finish**.

   ![UI for ASP.NET MVC Choosing the Products table in the database objects](https://www.telerik.com/aspnet-mvc/documentation/assets/9b3c6e48269198e2986c090f51010f6a/grid-binding-choose-database-objects.png)

5. Open `HomeController.cs` and edit the `Index` action method.

   ```
    public ActionResult Index()
    {
        var northwind = new NorthwindEntities();
        ViewBag.Products = northwind.Products;
        return View();
    }
   ```

6. In the view, configure the Grid for server binding to `ViewBag.Products`.

   ```ASPX
       <% Html.Kendo().Grid((IEnumerable<KendoGridServerDetailTemplate.Models.Product>)ViewBag.Products)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(product => product.ProductID);
               columns.Bound(product => product.ProductName);
           })
           .Render();
       %>
   ```

   ```Razor
       @(Html.Kendo().Grid((IEnumerable<KendoGridServerDetailTemplate.Models.Product>)ViewBag.Products)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(product => product.ProductID);
               columns.Bound(product => product.ProductName);
           })
       )
   ```

7. Set the detail template.

   ```ASPX
       <%: Html.Kendo().Grid((IEnumerable<KendoGridServerDetailTemplate.Models.Product>)ViewBag.Products)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(product => product.ProductID);
               columns.Bound(product => product.ProductName);
           })
           .Pageable()
           .DetailTemplate(product => {
           %>
               <div>ProductID: <%: product.ProductID %></div>
               <div>ProductName: <%: product.ProductName %></div>
               <div>UnitsInStock: <%: product.UnitsInStock %></div>
               <div>UnitPrice: <%: product.UnitPrice %></div>
               <div>UnitsOnOrder: <%: product.UnitsOnOrder %></div>
               <div>Discontinued: <%: product.Discontinued %></div>
           <%
           })
           .Render();
       %>
   ```

   ```Razor
       @(Html.Kendo().Grid((IEnumerable<KendoGridServerDetailTemplate.Models.Product>)ViewBag.Products)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(product => product.ProductID);
               columns.Bound(product => product.ProductName);
           })
           .Pageable()
           .DetailTemplate(@<text>
               <div>ProductID: @item.ProductID</div>
               <div>ProductName: @item.ProductName</div>
               <div>UnitsInStock: @item.UnitsInStock</div>
               <div>UnitPrice: @item.UnitPrice</div>
               <div>UnitsOnOrder: @item.UnitsOnOrder</div>
               <div>Discontinued: @item.Discontinued</div>
           </text>)
       )
   ```

8. Build and run the project.

   ![UI for ASP.NET MVC The final result is a Grid with a detail template](https://www.telerik.com/aspnet-mvc/documentation/assets/fc491596143fedb7d7beaf614294698b/grid-detail-template.png)

To download the Visual Studio Project, refer to [this GitHub repository](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridHierarchyServerDetailTemplate).

## Setting the Server Hierarchy

1. Create a new ASP.NET MVC 5 application. If you have installed the [Telerik UI for ASP.NET MVC Visual Studio Extensions](https://www.telerik.com/aspnet-mvc/documentation/vs-integration/introduction), create a Telerik UI for ASP.NET MVC application. Name the application `KendoGridServerDetailTemplate`. If you decided not to use the Telerik UI for ASP.NET MVC Visual Studio Extensions, follow the steps from the [First Steps article](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps) to add Telerik UI for ASP.NET MVC to the application.

2. Add a new `Entity Framework Data Model`. Right-click the `~/Models` folder in the solution explorer and pick **Add** > **New Item**. Choose **Data** > **ADO.NET Entity Data Model** in the **Add New Item** dialog. Name the model `Northwind.edmx` and click **Next**. This starts the **Entity Data Model Wizard**.

   ![UI for ASP.NET MVC A new entity data model](https://www.telerik.com/aspnet-mvc/documentation/assets/1286540ae776a0b9803b365ff9a0c775/grid-entity-data-model.png)

3. Select **Generate from database** and click **Next**. Configure a connection to the **Northwind** database. Click **Next**.

   ![UI for ASP.NET MVC Choosing the connection](https://www.telerik.com/aspnet-mvc/documentation/assets/1286540ae776a0b9803b365ff9a0c775/grid-entity-data-model.png)

4. Choose the **Products** and **Categories** tables from the **Which database objects do you want to include in your model?**. Leave all other options as they are set by default. Click **Finish**.

5. Open `HomeController.cs` and edit the `Index` action method.

   ```
    public ActionResult Index()
    {
        var northwind = new NorthwindEntities();
        ViewBag.Categories = northwind.Categories;
        return View();
    }
   ```

6. In the view, configure the Grid for server binding to `ViewBag.Categories`.

   ```ASPX
       <% Html.Kendo().Grid((IEnumerable<KendoGridServerHierarchy.Models.Category>)ViewBag.Categories)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(category => category.CategoryID);
               columns.Bound(category => category.CategoryName);
           })
           .Render();
       %>
   ```

   ```Razor
       @(Html.Kendo().Grid((IEnumerable<KendoGridServerHierarchy.Models.Category>)ViewBag.Categories)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(category => category.CategoryID);
               columns.Bound(category => category.CategoryName);
           })
       )
   ```

7. Set the detail template. Define another Grid which is bound to the `Products` property of the category entity. Make sure the name of the Grid is unique.

   ```ASPX
       <% Html.Kendo().Grid((IEnumerable<KendoGridServerHierarchy.Models.Category>)ViewBag.Categories)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(category => category.CategoryID);
               columns.Bound(category => category.CategoryName);
           })
           .DetailTemplate(category => {
           %>
               <% Html.Kendo().Grid(item.Products)
                   .Name(string.Format("product_grid_{0}", item.CategoryID)) // The Name() should be unique.
                   .Columns(columns =>
                   {
                       columns.Bound(product => product.ProductID);
                       columns.Bound(product => product.ProductName);
                   })
                   .Pageable()
                   .Render();
               %>
           <%
           })
           .Render();
       %>
   ```

   ```Razor
       @(Html.Kendo().Grid((IEnumerable<KendoGridServerHierarchy.Models.Category>)ViewBag.Categories)
           .Name("grid")
           .Columns(columns =>
           {
               columns.Bound(category => category.CategoryID);
               columns.Bound(category => category.CategoryName);
           })
           .Pageable()
           .DetailTemplate(@<text>
               @(Html.Kendo().Grid(item.Products)
                   .Name(string.Format("product_grid_{0}", item.CategoryID)) // The Name() should be unique.
                   .Columns(columns =>
                   {
                       columns.Bound(product => product.ProductID);
                       columns.Bound(product => product.ProductName);
                   })
                   .Pageable()
               )
           </text>)
       )
   ```

8. Build and run the project.

   ![UI for ASP.NET MVC The final result is a hierarchical Grid](https://www.telerik.com/aspnet-mvc/documentation/assets/3bb95dc3f447579ad6424bdba9d8d9f0/grid-hierarchy.png)

## See Also

* [Server Detail Template by the Grid HtmlHelper for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/grid/serverdetails)
* [Server-Side API](https://www.telerik.com/aspnet-mvc/documentation/api/grid)
