Working With Kendo UI TreeList And Custom Template In ASP.NET MVC 5 Web Application

Introduction
 
 In this article, I will demonstrate how to work with KendoTreeList View and create a custom template with KendoTreeList in ASP.NET MVC5 using Kendo UI and EF. After going through with this article, you may get the benefit of KendoTreeList's rich features. Okay! let's move forward.
 
Prerequisites  
  • Visual Studio
  • SQL Server
  • Basic knowledge of ASP.NET MVC
  • Basic knowledge of Entity Framework
  • Basic knowledge of jQuery
  • Basic knowledge of CSS
Article Flow 
  • Create a table in database with dummy values
  • Create an ASP.NET MVC Empty project
  • Configure Entity Framework with database and application
  • Create a Controller and View
  • Enable Kendo UI Features
  • Custom Template with TreeList
Create a table in database with dummy values
 
First, we will create a table in SQL Server to populate a Kendo UI TreeList with data in ASP.NET MVC. I have created a table "Employee" with the following design.

 

Execute the below query to create a table with the above design. 
  1. CREATE TABLE [dbo].[Employee](  
  2. [ID] [bigint] IDENTITY(1,1) NOT NULL,  
  3. [Name] [nvarchar](150) NULL,  
  4. [Designation] [nvarchar](150) NULL,  
  5. [Location] [nvarchar](50) NULL,  
  6. [TeamLeader] [bigintNULL,  
  7. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED  
  8. (  
  9.    [ID] ASC  
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  11. ON [PRIMARY]  
  12. GO  
And, insert a few employee records like below.
 
 
 
Execute the below query to get the same records.
  1. GO  
  2. SET IDENTITY_INSERT [dbo].[Employee] ON  
  3. GO  
  4. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (1, N'Gnanavel Sekar', N'Software Engineer', N'Chennai', 4)  
  5. GO  
  6. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (2, N'Robert Bellar', N'Software Engineer', N'Chennai', 4)  
  7. GO  
  8. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (3, N'Muthu A', N'Software Engineer', N'Chennai', 4)  
  9. GO  
  10. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (4, N'Ramar Ammavasi', N'Sr.Software Engineer', N'Chennai'NULL)  
  11. GO  
  12. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (5, N'Gokul D', N'Sr.Software Engineer', N'Coimbatore'NULL)  
  13. GO  
  14. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (6, N'Karthick', N'Software Engineer', N'Coimbatore', 5)  
  15. GO  
  16. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (7, N'Sharma', N'Software Engineer', N'Coimbatore', 5)  
  17. GO  
  18. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (8, N'Sheriff', N'Software Engineer', N'Coimbatore', 5)  
  19. GO  
  20. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (9, N'Anand s', N'Software Engineer', N'Coimbatore', 5)  
  21. GO  
  22. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location], [TeamLeader]) VALUES (10, N'Mubarak', N'Software Engineer', N'Coimbatore', 5)  
  23. GO  
  24. SET IDENTITY_INSERT [dbo].[Employee] OFF  
  25. GO  
Create an ASP.NET MVC Empty project  
 
To create ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2013. 
  1. Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it as "KendoTreeListView".
  2. Now, click OK.
  3. Then, select Empty MVC template and click OK to create the project.
  4. Once you click OK, the project will be created with the basic architecture of MVC.
  5. If you are not aware of how to create an Empty ASP.NET Web Application, please visit  Step 1 and Step 2 to learn. Once you complete these steps, you will get the screen as below.
 
Configure Entity Framework with database and application
 
Here, I have already discussed how to configure and implement database first approach. In the meantime, choose our created table with entity framework. Once we finish our configuration with SQL table "Employee" from CSharpCorner database and with our application, we will get the below screen as succeeding configuration.
 
 
 
Create a Controller and View
 
Now, create an empty Controller and View. Here, I created a Controller with the name of "KendoTreeListController". Whenever we create an empty Controller, it is created with empty Index action method. And, create an empty View to this action method "Index". 
 
Enable Kendo UI Features
 
Here, we are going to enable the Kendo UI features with our application by adding the below CSS and JS in our shared _Layout View.
  1. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.common-material.min.css" />  
  2. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.min.css" />  
  3. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.mobile.min.css" />  
  4. <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/jquery.min.js"></script>  
  5. <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/kendo.all.min.js"></script>  
Now, write the logic in a Controller to load the employee(s) from a database using Entity Framework. Add the below code to load it.
  1. [HttpGet]  
  2. public JsonResult LoadEmployeesData() {  
  3.     using(CSharpCornerEntities Entities = new CSharpCornerEntities()) {  
  4.         var employees = Entities.Employees.ToList();  
  5.         return Json(employees, JsonRequestBehavior.AllowGet);  
  6.     }  
And now, create a div in the View which will act as TreeList.
  1. <div id="treelist" style="width:610px"></div>   
Now, call the LoadEmployeesData action method to bind the data to TreeList Datasource.
  1. <script>  
  2.     $(document).ready(function() {  
  3.         $("#treelist").kendoTreeList({  
  4.             dataSource: {  
  5.                 transport: {  
  6.                     read: {  
  7.                         url: "http://localhost:28655/KendoTreeList/LoadEmployeesData",  
  8.                         dataType: "json"  
  9.                     }  
  10.                 },  
  11.                 schema: {  
  12.                     model: {  
  13.                         id: "ID",  
  14.                         parentId: "TeamLeader",  
  15.                         fields: {  
  16.                             TeamLeader: {  
  17.                                 field: "TeamLeader",  
  18.                                 nullable: true  
  19.                             },  
  20.                         },  
  21.                         expanded: true  
  22.                     }  
  23.                 }  
  24.             },  
  25.             height: 500,  
  26.             filterable: true,  
  27.             sortable: true,  
  28.             columns: [{  
  29.                 field: "Name",  
  30.                 title: "Employee Name",  
  31.                 width: 200  
  32.             }, {  
  33.                 field: "Designation",  
  34.                 title: "Designation",  
  35.                 width: 200  
  36.             }, {  
  37.                 field: "Location"  
  38.             }]  
  39.         });  
  40.     });  
  41. </script>  
Detailed Description
 
$("#treelist").kendoTreeList() -> Here, we are enabling the Kendo UI TreeList to div "treelist" and it will act as the KenoTreeList.
dataSource: { transport: {read: { -> This Transport helps us to read the data from the desired services.
Here, we are loading the data from our MVC controller. In the above code, you can see that we mentioned the URL of our action.
The next parameter says what type of data we are reading and binding to the data source. Schema models represent which field will act as the parent for others. Here, we mentioned the parent field as Team Leader. Columns represent the fields need to be displayed in Kendo TreeList.
 
Run your application.
 
Expand
 
 
 
Sortable and Filterable 
 
In the above code, you can see that we have enabled these features by setting true.
 
 
 
Now, let's add the employee photos with the names using Kendo custom template.
 
Custom Template with TreeList
 
We will append the employee photos based on database ID column, and I have copied static images to the physical folder under a content folder named Employees. 
 
  
 
Add the below Kendo Template in your View. In the below code, you can see that we are appending the employee photos from Employees folder under content folder from the server. Whenever we get the data into the template, we need to use the data as an object and # for column representation.
  1. <script id="photo-template" type="text/x-kendo-template">  
  2.     <div class='employee-photo' style='background-image: url(../Content/Employees/#:data.ID#.jpg); '></div>  
  3.     <div class='employee-name'>#: Name #</div>  
  4. </script>  
Now, append this photo template with columns. In the below code, you can see that we mentioned appending with Employee name and rest of the things are same as earlier. 
  1. columns: [{  
  2.     field: "Name",  
  3.     title: "Employee Name",  
  4.     width: 230,  
  5.     template: $("#photo-template").html()  
  6. }, {  
  7.     field: "Designation",  
  8.     title: "Designation",  
  9.     width: 200  
  10. }, {  
  11.     field: "Location"  
  12. }]  
Add the below CSS to give rich looks to your Photo template 
  1. <style>  
  2.     .employee-photo {  
  3.         display: inline-block;  
  4.         width: 32px;  
  5.         height: 32px;  
  6.         border-radius: 50%;  
  7.         background-size: 32px 35px;  
  8.         background-position: center center;  
  9.         vertical-align: middle;  
  10.         line-height: 32px;  
  11.         box-shadow: inset 0 0 1px #999, inset 0 0 10px rgba(0, 0, 0, .2);  
  12.         margin-left: 5px;  
  13.     }  
  14.   
  15.     .employee-name {  
  16.         display: inline-block;  
  17.         vertical-align: middle;  
  18.         line-height: 32px;  
  19.         padding-left: 3px;  
  20.     }  
  21. </style>  
Now, run your application.
 
 
 
Complete View _Layout.cshtml 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  7.     <title>@ViewBag.Title - Kendo TreeList</title>  
  8.     <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />  
  9.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />  
  10.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.common-material.min.css" />  
  11.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.min.css" />  
  12.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.mobile.min.css" />  
  13.     <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/jquery.min.js"></script>  
  14.     <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/kendo.all.min.js"></script>  
  15. </head>  
  16.   
  17. <body>  
  18.     <div class="navbar navbar-inverse navbar-fixed-top">  
  19.         <div class="container">  
  20.             <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  21. <span class="icon-bar"></span>  
  22. <span class="icon-bar"></span>  
  23. <span class="icon-bar"></span>  
  24. </button> @Html.ActionLink("Kendo UI TreeList with ASP.NET MVC5 and EF""Index""Home"nullnew { @class = "navbar-brand" }) </div>  
  25.             <div class="navbar-collapse collapse">  
  26.                 <ul class="nav navbar-nav"> </ul>  
  27.             </div>  
  28.         </div>  
  29.     </div>  
  30.     <div class="container body-content"> <br /> @RenderBody()  
  31.         <hr /> </div>  
  32. </body>  
  33.   
  34. </html>  
Index.cshtml 
  1. @ {  
  2.     ViewBag.Title = "Kendo-TreeList";  
  3. } < body > < div id = "example" > < div id = "treelist"  
  4. style = "width:610px" > < /div> < script id = "photo-template"  
  5. type = "text/x-kendo-template" > < div class = 'employee-photo'  
  6. style = 'background-image: url(../Content/Employees/#:data.ID#.jpg); ' > < /div> < div class = 'employee-name' > #: Name# < /div> < /script> < script > $(document).ready(function() {  
  7.     $("#treelist").kendoTreeList({  
  8.         dataSource: {  
  9.             transport: {  
  10.                 read: {  
  11.                     url: "http://localhost:28655/KendoTreeList/LoadEmployeesData",  
  12.                     dataType: "json"  
  13.                 }  
  14.             },  
  15.             schema: {  
  16.                 model: {  
  17.                     id: "ID",  
  18.                     parentId: "TeamLeader",  
  19.                     fields: {  
  20.                         TeamLeader: {  
  21.                             field: "TeamLeader",  
  22.                             nullable: true  
  23.                         },  
  24.                     },  
  25.                     expanded: true  
  26.                 }  
  27.             }  
  28.         },  
  29.         height: 500,  
  30.         filterable: true,  
  31.         sortable: true,  
  32.         columns: [{  
  33.             field: "Name",  
  34.             title: "Employee Name",  
  35.             width: 230,  
  36.             template: $("#photo-template").html()  
  37.         }, {  
  38.             field: "Designation",  
  39.             title: "Designation",  
  40.             width: 200  
  41.         }, {  
  42.             field: "Location"  
  43.         }]  
  44.     });  
  45. }); < /script> < style > .employee - photo {  
  46.         display: inline - block;  
  47.         width: 32 px;  
  48.         height: 32 px;  
  49.         border - radius: 50 % ;  
  50.         background - size: 32 px 35 px;  
  51.         background - position: center center;  
  52.         vertical - align: middle;  
  53.         line - height: 32 px;  
  54.         box - shadow: inset 0 0 1 px #999, inset 0 0 10px rgba(0,0,0,.2);  
  55. margin-left: 5px;  
  56. }  
  57. .employee-name {  
  58. display: inline-block;  
  59. vertical-align: middle;  
  60. line-height: 32px;  
  61. padding-left: 3px;  
  62. }  
  63. </style>  
  64. </div>  
  65. </body>  
Complete Controller 
  1. using KendoTreeListView.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace KendoTreeListView.Controllers {  
  8.     public class KendoTreeListController: Controller {  
  9.         //  
  10.         // GET: /KendoTreeList/  
  11.         public ActionResult Index() {  
  12.                 return View();  
  13.             }  
  14.             [HttpGet]  
  15.         public JsonResult LoadEmployeesData() {  
  16.             using(CSharpCornerEntities Entities = new CSharpCornerEntities()) {  
  17.                 var employees = Entities.Employees.ToList();  
  18.                 return Json(employees, JsonRequestBehavior.AllowGet);  
  19.             }  
  20.         }  
  21.     }  
  22. }  
RouteConfig.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace KendoTreeListView {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "KendoTreeList", action = "Index", id = UrlParameter.Optional  
  13.             });  
  14.         }  
  15.     }  
  16. }  
Here, we have created an empty project so it can not get built-in CSS. I have attached the respective CSS under Content folder and referred it to a project. Refer to the attached project for reference and I did attach the folders such as content, View, Controller and Model.
 
Summary 
 
In this article, we discussed how to work with TreeList in our ASP.NET MVC5 web application using Kendo UI and Entity Framework.
I hope it's helpful. Your valuable feedback and comments about this article are always welcome. 

Up Next
    Ebook Download
    View all
    Learn
    View all