Populate The Kendo Grid On Button Click Event In ASP.NET Application

Before going through this article ensure that you have a basic understanding of the MVC Architecture, ASP.NET Web API and jQuery.

Let us start with creating an ASP.NET WEB API Application.

Creating an Empty ASP.NET WEB API Project

Create a simple empty WEB API project as in the following figures:
 

 

Creating a Model Class

Right click on the model folder and add a new class, in my case I named it EmployeeList.cs:

Code in EmployeeList.cs:

  1. class EmployeeList  
  2.     {  
  3.         public EmployeeList(int EmpId, string EmployeeName, string Designation)  
  4.         {  
  5.             this.EmployeeID = EmpId;  
  6.             this.EmployeeName = EmployeeName;  
  7.             this.Designation = Designation;  
  8.         }  
  9.         public int EmployeeID { getset; }  
  10.         public string EmployeeName { getset; }  
  11.         public string Designation { getset; }  
  12.     }  

Creating a Controller

Right click on the Controller folder and add a new WEB API 2- Empty controller as in the figure 3, in my case I named it EmployeeController.cs:

Code in EmployeeController.cs
  1. [RoutePrefix("api/Employee")]  
  2.     public class EmployeeController : ApiController  
  3.     {  
  4.         [HttpGet]  
  5.         [AllowAnonymous]  
  6.         [Route("GetEmployeeList")]  
  7.         public HttpResponseMessage GetEmployee()  
  8.         {  
  9.             try {   
  10.             List<EmployeeList> _emp = new List<EmployeeList>();  
  11.             _emp.Add(new EmployeeList(1, "Arun""Software Engineer"));  
  12.             _emp.Add(new EmployeeList(2, "Pradeep""Infrastructure Engineer"));  
  13.             _emp.Add(new EmployeeList(3, "Jai""HR"));  
  14.             return Request.CreateResponse(HttpStatusCode.OK, _emp, Configuration.Formatters.JsonFormatter);  
  15.             }  
  16.             catch(Exception ex)  
  17.             {  
  18.                 return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  19.             }  
  20.   
  21.         }  
  22.   
  23.     }  

The above employee controller action will return an employee list as a response.

API Test 
 
Test the API using the POSTMAN/Fiddler as in the following figure 4:
 
API End Point: /api/Employee/GetEmployeeList 
 
 
 
The API is working fine, now it's ready to consume.

Using a Kendo Grid with MVVM pattern

Please read my previous article to get more details about implementing Kendo Grid.

Creating a HTML page

Create a new HTML page in the project.

Design:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.common.min.css">  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.rtl.min.css">  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.default.min.css">  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.mobile.all.min.css">  
  10.   
  11.     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2016.1.226/js/angular.min.js"></script>  
  13.     <script src="http://kendo.cdn.telerik.com/2016.1.226/js/jszip.min.js"></script>  
  14.     <script src="http://kendo.cdn.telerik.com/2016.1.226/js/kendo.all.min.js"></script><script src="JavaScript/Model/MemReportCore.js"></script>  
  15.     <script src="JavaScript/ViewModel/MemberReportViewModel.js"></script>  
  16. </head>  
  17. <body>  
  18.     <style>  
  19.         .k-input {  
  20.             font-weight: normal;  
  21.         }  
  22.     </style>  
  23.     <div class="col-lg-12">  
  24.         <div id="Main" class="main"></div>  
  25.     </div>  
  26.   
  27.     <script type="text/x-kendo-template" id="Layout-temp">  
  28.         <div class="col-lg-12">  
  29.             <div id="content"></div>  
  30.         </div>  
  31.     </script>  
  32.     <script type="text/x-kendo-template" id="Dashboard-temp">  
  33.         <div class="row">  
  34.             <button id="Btn" data-bind="click:DisplayGrid">Show Grid</button>  
  35.             <div class=row>  
  36.                 <div id="grid" data-role="grid" data-filterable="true" data-pegeable="true"  
  37.                      data-columns="[  
  38.         {'field':'EmployeeName','title':'Name'},  
  39.         {'field':'Designation','title':'Designation'},  
  40.           
  41.   
  42.         ]"  
  43.                      data-bind="source: dataSource,visible: isVisible," style="height: 200px"  
  44.         </div>  
  45.             </div>  
  46.         </div>  
  47.         </script>  
  48. </body>  
  49. </html>  

Creating a JavaScript files

  1. View Model: Create a JavaScript file, in my case I named it ViewModel.Js, The View-Model is a representation of your data (the Model) which will be displayed in the View.
ViewModel.Js
  1. (function (G, $, undefined) {  
  2.   
  3.     $.extend(true, G, {  
  4.         KendoGrid: {  
  5.             ViewModel: {  
  6.                 DashboardModel: new kendo.observable({  
  7.                     isVisible: false,  
  8.   
  9.                         DisplayGrid: function (e) {  
  10.                         e.preventDefault();  
  11.                         G.KendoGrid.ViewModel.DashboardModel.set("isVisible"true);  
  12.                         $.ajax(  
  13.                                     {  
  14.                                              type: 'GET',  
  15.                                              url: '/api/Employee/GetEmployeeList',  
  16.                                              dataType: 'json',  
  17.   
  18.                      success: function (result) {  
  19.   
  20.                              $("#grid").data("kendoGrid").dataSource.data(result);  
  21.                                                     }  
  22.                             });  
  23.   
  24.   
  25.   
  26.                         var grid = $("#grid").data("kendoGrid");  
  27.   
  28.                         grid.dataSource.read();  
  29.   
  30.                     },  
  31.                 }),
  32.   
  33.             }  
  34.         }  
  35.     });  
  36. })(window.Gni = window.Gni || {}, jQuery);  
Initially the Kendo grid is hidden by setting the visible property as false.

From the above script you can observe that the DisplayGrid function which will fire when the button is clicked is used to set the visible property to true and to  bind the ajax call result with datasource of the Kendo grid.

Core: Create a JavaScript file, in my case I named it Core.Js.

This core script is used to start the rendering of the templates and calls the view model to bind the data in the UI.

Core.js

  1. $(function () {  
  2.     var Layout = new kendo.Layout("Layout-temp");  
  3.     var DashboardView = new kendo.View("Dashboard-temp", { model: Gni.KendoGrid.ViewModel.DashboardModel });  
  4.     var router = new kendo.Router({  
  5.         init: function () {  
  6.             Layout.render("#Main");  
  7.             Layout.showIn("#content", DashboardView);  
  8.   
  9.         }  
  10.     });  
  11.     router.start();  
  12.      
  13. });  

The Result in Browser

 
Click the button to display the Kendo Grid.
 
                                                                               
 
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcomed.
 
Read more articles on jQuery

Next Recommended Readings