CRUD In Kendo TreeList View Using ASP.NET WEB API

Introduction

This article tells about how to perform CRUD in Kendo TreeList View, using ASP.NET Web API. To explain it, I have created a RESTful GET Service which is used to load the DataSource of Kendo TreeList

Requirements

  • VS 2010 and above
  • SQL Server 2008 and above
Before going through this article, please go through my previous article to get a basic idea to implement the Kendo TreeList View.

Prerequisites

Basic knowledge of ASP.NET WebAPI, jQuery, 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 a HTML page and implementing CRUD IN kendo Tree List View

Set up the Table

For this article, I have created one table, design of which is shown below.


Figure 1 
 
Employee Table 
 

Figure 2 
 
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 “TreeViewCrud".


Figure 3


Figure 4 

Creating model classes

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 as Employee) and click Add. 

Step 2

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


Figure 5 

Step 3

Click the "New Connection" button. The "Connection Properties" window will open.


Figure 6
 
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 "Employee" from the available databases and click OK.

Step 5

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

Step 6

Select the table to generate the models for Employee table and click "Finish".


Figure 7

My database schema is shown in the figure given below.

Figure 8

Creating a Controller

Right click on Controller folder and add a new Web API 2 controller with actions using entity framework, as shown in the Figure 9. In my case, I named it as EmployeesController.cs.


Figure 9 

 
Figure 10 

EmployeeController.cs

  1. public class EmployeesController : ApiController  
  2. {  
  3.     private EmployeeEntities db = new EmployeeEntities();  
  4.   
  5.     // GET: api/Employees  
  6.     public IQueryable<Employee> GetEmployees()  
  7.     {  
  8.         return db.Employees;  
  9.     }  
  10.   
  11.     // GET: api/Employees/5  
  12.     [ResponseType(typeof(Employee))]  
  13.     public IHttpActionResult GetEmployee(int id)  
  14.     {  
  15.         Employee employee = db.Employees.Find(id);  
  16.         if (employee == null)  
  17.         {  
  18.             return NotFound();  
  19.         }  
  20.   
  21.         return Ok(employee);  
  22.     }  
  23.   
  24.     // PUT: api/Employees/5  
  25.     [ResponseType(typeof(void))]  
  26.     public IHttpActionResult PutEmployee(Employee employee)  
  27.     {  
  28.         if (!ModelState.IsValid)  
  29.         {  
  30.             return BadRequest(ModelState);  
  31.         }  
  32.         
  33.         db.Entry(employee).State = EntityState.Modified;  
  34.   
  35.         try  
  36.         {  
  37.             db.SaveChanges();  
  38.         }  
  39.         catch (DbUpdateConcurrencyException)  
  40.         {  
  41.             if (!EmployeeExists(employee.EmployeeID))  
  42.             {  
  43.                 return NotFound();  
  44.             }  
  45.             else  
  46.             {  
  47.                 throw;  
  48.             }  
  49.         }  
  50.   
  51.         return StatusCode(HttpStatusCode.NoContent);  
  52.     }  
  53.   
  54.     // POST: api/Employees  
  55.     [ResponseType(typeof(Employee))]  
  56.     public IHttpActionResult PostEmployee(Employee employee)  
  57.     {  
  58.         if (!ModelState.IsValid)  
  59.         {  
  60.             return BadRequest(ModelState);  
  61.         }  
  62.   
  63.         db.Employees.Add(employee);  
  64.         db.SaveChanges();  
  65.   
  66.         return CreatedAtRoute("DefaultApi"new { id = employee.EmployeeID }, employee);  
  67.     }  
  68.   
  69.     // DELETE: api/Employees/5  
  70.     [ResponseType(typeof(Employee))]  
  71.     public IHttpActionResult DeleteEmployee(Employee _employee)  
  72.     {  
  73.         Employee employee = db.Employees.Find(_employee.EmployeeID);  
  74.         if (employee == null)  
  75.         {  
  76.             return NotFound();  
  77.         }  
  78.   
  79.         db.Employees.Remove(employee);  
  80.         db.SaveChanges();  
  81.   
  82.         return Ok(employee);  
  83.     }  
  84.   
  85.     protected override void Dispose(bool disposing)  
  86.     {  
  87.         if (disposing)  
  88.         {  
  89.             db.Dispose();  
  90.         }  
  91.         base.Dispose(disposing);  
  92.     }  
  93.   
  94.     private bool EmployeeExists(int id)  
  95.     {  
  96.         return db.Employees.Count(e => e.EmployeeID == id) > 0;  
  97.     }  
  98. }  

Creating a HTML page

Create one new HTML page in the application where we are going to implement the kendoTreeList View, using the RESTful Service. In my case, I named it as TreeListView.html.

CRUD action in kendoTreeList

The kendoTreeList View is used to display the self-referencing tabular data and allows sorting, filtering, and editing. We can use an empty div tag and initialize the TreeViewList settings from the script. 

Write the below code in TreeListView.html 

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   
  5.     <style>  
  6.         html {  
  7.             font-size: 14px;  
  8.             font-family: Arial, Helvetica, sans-serif;  
  9.         }  
  10.     </style>  
  11.     <title></title>  
  12.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.common-material.min.css" />  
  13.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.material.min.css" />  
  14.     <link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.1118/styles/kendo.material.mobile.min.css" />  
  15.   
  16.     <script src="//kendo.cdn.telerik.com/2016.3.1118/js/jquery.min.js"></script>  
  17.     <script src="//kendo.cdn.telerik.com/2016.3.1118/js/kendo.all.min.js"></script>  
  18. </head>  
  19. <body>  
  20.     <h3>Kendo TreeList View using remote data </h3>  
  21.     <br />  
  22.     <br />  
  23.     <div id="example">  
  24.         <div id="treelist"></div>  
  25.   
  26.         <script>  
  27.                 $(document).ready(function () {  
  28.   
  29.                     var dataSource = new kendo.data.TreeListDataSource({  
  30.                         transport: {  
  31.                             read: {  
  32.                                 url: "api/Employees",  
  33.                                 dataType: "json",  
  34.                                 type:"GET"  
  35.                             },  
  36.                             update: {  
  37.                                 url: "api/Employees/",  
  38.                                 dataType: "json",  
  39.                                 type:"PUT"  
  40.                             },  
  41.                             destroy: {  
  42.                                 url: "api/Employees/",  
  43.                                 dataType: "json",  
  44.                                 type:"DELETE"  
  45.                             },  
  46.                             create: {  
  47.                                 url: "api/Employees",  
  48.                                 dataType: "json",  
  49.                                 type:"POST",  
  50.                               
  51.                             parameterMap: function(options, operation) {  
  52.                                 if (operation !== "read" && options.models) {  
  53.                                     return {models: kendo.stringify(options.models)};  
  54.                                 }  
  55.                             }  
  56.                         },  
  57.                             },  
  58.                             schema: {  
  59.                                 model: {  
  60.                                     id: "EmployeeID",  
  61.                                     parentId: "ReportTo",  
  62.                                     fields: {  
  63.                                         EmployeeID: { type: "number", nullable: false },  
  64.                                         ReportTo: { field: "ReportTo", nullable: true }  
  65.                                     }  
  66.                                 }  
  67.                             }  
  68.                         });  
  69.   
  70.                     $("#treelist").kendoTreeList({  
  71.                         dataSource: dataSource,  
  72.                         toolbar: ["create"],  
  73.                         filterable: true,  
  74.                         editable: true,  
  75.                         sortable:true,  
  76.                         columns: [  
  77.                             { field: "FirstName", expandable: true, title: "FirstName", width: 250 },  
  78.                             { field: "LastName", title: "LastName", width: 250 },  
  79.                             { field: "Designation", title: "Designation", width: 250 },  
  80.                             {  
  81.                                 title: "Edit", command: ["edit""destroy"], width: 250,  
  82.                                 attributes: {  
  83.                                     style: "text-align: center;"  
  84.                                 }  
  85.                             }  
  86.   
  87.                         ]  
  88.                     });  
  89.                 });  
  90.         </script>  
  91.     </div>  
  92.   
  93.   
  94.   
  95. </body>  
  96. </html>   
Result
 
Read 
 
Url api/Employee
Type GET
 
 Add a New Record
 
Url - api/Employee
Type - POST  
 
Click on "Add New Record" button in the toolbar of kendo Grid. It will create a new row in the TreeListView to add the entry .
 

Update

Url - api/Employee
Type - PUT 
 
Click on "Edit" command to edit the data in particular row.
 

Delete
 
Url - api/Employee
Type - DELETE 
 
Click on "Delete" custom button  to delete a particular record.


I hope, you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.
 
Get the Source code from Github.


Similar Articles