Kendo Panel Bar Using ASP.NET WEB API And Entity Framework

Introduction

This article explains how to implement remote data binding in Kendo panel bar, using ASP.NET Web API Application. To explain it, I have created a RESTful GET Service, using ASP.NET Web API, which is used to load the DataSource of Kendo panel bar.

Prerequisites

Basic knowledge of ASP.NET Web API, jQuery, and Kendo UI.

This article flows, as per the following.

  1. Creating an ASP.NET Web API Application.
  2. Creating a Controller.
  3. Testing the REST API.
  4. Creating HTML page and implementing Kendo panel bar with remote data binding

Creating an ASP.NET WEB API Application

Create a Web API Application, using an installed Web template in Visual Studio, as shown below. In my case, I named the Application “KendoPanelBar".

 
Figure 1 

 
Figure 2 

Creating Model Class

Now, we will create Entity Framework models from the database tables.

Step 1

Right-click the Models folder, select Add -> ADO.NET Entity Data Model or select Add->New Item. In the Add New Item Window, select data in the left pane and ADO.NET Entity Data Model from the center pane. Name the new model file. In my case, I made it Employee, and click Add.

Step 2

In the Entity Data Model Wizard, select EF Designer from the database and click Next.

 
 
Figure 3

Step 3

Click the New Connection button. The Connection Properties Window will open.

 
Figure 4 

Step 4 

In the Connection Properties Window, provide the name of the local Server, where the database was created (in this case (DESKTOP-585QGBN)). After providing the Server name, select the Employee from the available databases and click OK.

 
Figure 5

Step 5 

You can use the default name for the connection to save the Web.Config file and click Next

 
Figure 6

Step 6

Select the table to generate models for Country, State and City table and click Finish.

 
 
Figure 7 

My database schema is shown in the figure, which is given below.

 
Figure 8 

SQL Table

 
 
Figure 9 

Creating a Controller

Create a new empty Controller. Right-click the Controllers folder and select Add –> New Empty Controller. In my case, I named it as EmplpoyeController.cs.

EmployeeController.cs

  1. [RoutePrefix("api/Employee")]  
  2. public class EmployeeController: ApiController {  
  3.     EmployeeEntities db = new EmployeeEntities();  
  4.     [HttpGet]  
  5.     [AllowAnonymous]  
  6.     [Route("EmployeeList")]  
  7.     public HttpResponseMessage GetEmployee() {  
  8.             try {  
  9.                 var Result = db.Employees.Where(e => e.ReportTo == null);  
  10.                 return Request.CreateResponse(HttpStatusCode.OK, Result, Configuration.Formatters.JsonFormatter);  
  11.             } catch (Exception ex) {  
  12.                 return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  13.             }  
  14.         }  
  15.         [HttpGet]  
  16.         [AllowAnonymous]  
  17.         [Route("EmployeeList")]  
  18.     public HttpResponseMessage GetEmployee(int ? EmployeeID) {  
  19.         try {  
  20.             if (EmployeeID == null) {  
  21.                 var Result = db.Employees.Where(e => e.ReportTo == null);  
  22.                 return Request.CreateResponse(HttpStatusCode.OK, Result, Configuration.Formatters.JsonFormatter);  
  23.             } else {  
  24.                 var Result = db.Employees.Where(e => e.ReportTo == EmployeeID.Value);  
  25.                 return Request.CreateResponse(HttpStatusCode.OK, Result, Configuration.Formatters.JsonFormatter);  
  26.             }  
  27.         } catch (Exception ex) {  
  28.             return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  29.         }  
  30.     }  
  31. }  

 

GetEmployee Action is used to get the employee details whose report to property is equal to null. GetEmployee Action with the paramter EmployeeID is used to fetch the employee detail, which is based on their report to value.

Testing the REST API

Test API, using POSTMAN/Fiddler, as shown in the figure given below.

API End Point - /api/Employee/EmployeeList.

Type - GET.

 
Figure 10 

API End Point - /api/Employee/EmployeeList?EmployeeID=[EmployeeID]

Type - GET.

 
Figure 11 

 
Figure 12

Creating HTML page and implementing Kendo panel bar with remote data binding

Create a new HTML page in the Application, where we are going to implement Kendo panel bar. In my case, I named it PanelBar.html

PanelBar.html
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Kendo Panel Bar</title>  
  6.     <meta charset="utf-8" />  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css">  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css">  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.default.min.css">  
  10.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css">  
  11.     <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/angular.min.js"></script>  
  13.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/jszip.min.js"></script>  
  14.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  15. </head>  
  16.   
  17. <body>  
  18.     <h3>Kendo Panel Bar</h3>  
  19.     <div id="example">  
  20.         <div class="demo-section k-content">  
  21.             <div id="panelbar"></div>  
  22.         </div>  
  23.         <script>  
  24.             var homogeneous = new kendo.data.HierarchicalDataSource({  
  25.                 transport: {  
  26.                     read: {  
  27.                         url: "/api/Employee/EmployeeList",  
  28.                         dataType: "json"  
  29.                     }  
  30.                 },  
  31.                 schema: {  
  32.                     model: {  
  33.                         id: "EmployeeID",  
  34.                         hasChildren: "HasEmployees"  
  35.                     }  
  36.                 }  
  37.             });  
  38.             $("#panelbar").kendoPanelBar({  
  39.                 dataSource: homogeneous,  
  40.                 dataTextField: "FirstName"  
  41.             });  
  42.         </script>  
  43.     </div>  
  44. </body>  
  45.   
  46. </html>  
From the code given above, it is obvious that we are using HierarchicalDataSource to implmenent the child and parent relationship, which is based on HasEmployees property value in the PanelBar.

Result in Browser

 

I hope, you have enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all