Kendo UI Grid With ASP.NET MVC 5, Entity Framework, And Web API - Part One

This article will demonstrate how to work on Kendo UI Grid with ASP.Net MVC5, Entity Framework, and Web-API.

Introduction

This article will demonstrate how to work on Kendo UI Grid with ASP.Net MVC5, Entity Framework, and Web-API. First, we will see how to load the data from the database using Entity framework and ASP.Net MVC to Kendo grid. Nowadays no one is ready to sit and write the code for a long time. For that we find many predefined features and third-party tools; in the same way here we are going to use the Kendo UI features to reduce our coding time and also to bring the rich look for our UI with less customization. Okay, we will see these all in this article and more in upcoming articles. Let's move forward. 

Article Flow 
  • Create a table with dummy values in Sqlserver
  • Create ASP.Net MVC 5 empty project
  • Create ASP.Net WEB-API empty project
  • Configure Entity Framework in ASP.Net Web API
  • Create Controller with data Load Logic using Entity Framework in ASP.Net Web API
  • Configure Kendo UI with ASP.Net MVC Application
  • Create View with Kendo UI Grid
  • Startup Project
Create a table with dummy values in Sqlserver

I have created a table "Employee" with the following design.

To create this employee table, execute the below query.
  1. CREATE TABLE [dbo].[Employee](  
  2. [ID] [int] IDENTITY(1,1) NOT NULL,  
  3. [Name] [nvarchar](200) NOT NULL,  
  4. [Designation] [nvarchar](200) NOT NULL,  
  5. [Department] [nvarchar](200) NOT NULL,  
  6. [YOE] [intNOT NULL,  
  7. [Status] [bitNOT NULL,  
  8. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED  
  9. (  
  10.    [ID] ASC  
  11. ))  
And now, add a few dummy values to populate in Kendo UI Grid. I have added some rows as shown below.

 

To add these dummy values, execute the below insert queries.
  1. GO  
  2. SET IDENTITY_INSERT [dbo].[Employee] ON  
  3. GO  
  4. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Department], [YOE], [Status]) VALUES (1, N'Gnanavel Sekar', N'Software Engineer', N'IT', 3, 1)  
  5. GO  
  6. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Department], [YOE], [Status]) VALUES (2, N'Ramar Ammavasi', N'Tech Lead', N'IT', 7, 1)  
  7. GO  
  8. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Department], [YOE], [Status]) VALUES (3, N'Robert B', N'Application Developer', N'IT', 2, 1)  
  9. GO  
  10. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Department], [YOE], [Status]) VALUES (4, N'Ammaiyappan I', N'Software Engineer', N'IT', 3, 1)  
  11. GO  
  12. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Department], [YOE], [Status]) VALUES (5, N'Subash S', N'Team Lead', N'BPO', 5, 1)  
  13. GO  
  14. SET IDENTITY_INSERT [dbo].[Employee] OFF  
  15. GO  
Create ASP.Net MVC 5 empty project

To create ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2013.

Step 1 Select Language and Name Your Project

Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it "Application". Now, click OK.

 

Step2

Select Empty MVC project and click OK to create the project.

 

Step 3

Once you click OK, the project will be created with the basic architecture of MVC.

 

Create ASP.Net WEB-API empty project

To create a new ASP.NET Web API project, follow the below steps one by one.

Step 1

Select New Project -> Web -> ASP.NET Web Application. Name your project (Here, I mentioned it as "API") and click OK. This step is common for MVC, WebAPI, and WebForms.

 

Step 2

Now, select Empty Web API Project and click OK.

 

Step 3

In the below image, you can see that both the projects have been created with the basic architecture of WebAPI and MVC.

 

Configure Entity Framework in ASP.Net Web API

Here, I already discussed how to configure and implement database first approach. In the meantime choose our created table with entity framework, once we did our configuration with SQL table "Employee" from CRUD database and with our application you will get the below screen as succeeding configuration.

 

Create Controller with data Load Logic using Entity Framework in ASP.Net Web API

Step 1

Add Controller and name it as "CRUD"

 

Step 2 Write logic to load data from database 

To load data from database write the below codes to in CRUD controller under in API project, in below code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. using System.Web.Http.Description;  
  9. using System.Web.Mvc;  
  10. namespace Web_API.Controllers {  
  11.     /// <summary>  
  12.     /// Crud Operation Controller  
  13.     /// </summary>  
  14.     public class CRUDController: ApiController {  
  15.         private static CRUDEntities crud = new CRUDEntities();  
  16.         /// <summary>  
  17.         /// Get All Employee Details  
  18.         /// </summary>  
  19.         /// <returns></returns>  
  20.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  21.         [System.Web.Http.Route("api/GetEmployees")]  
  22.         [System.Web.Http.HttpGet]  
  23.         public HttpResponseMessage GetEmployees() {  
  24.             var result = crud.Employees.ToList();  
  25.             return GetResultResponse(result);  
  26.         }  
  27.         /// <summary>  
  28.         /// Get Response for Each result  
  29.         /// </summary>  
  30.         /// <param name="Result"></param>  
  31.         /// <returns></returns>  
  32.         public HttpResponseMessage GetResultResponse(object Result) {  
  33.             HttpResponseMessage response = null;  
  34.             try {  
  35.                 response = Request.CreateResponse(HttpStatusCode.OK, Result);  
  36.             } catch (Exception ex) {  
  37.                 response = Request.CreateResponse(HttpStatusCode.InternalServerError, Result);  
  38.             }  
  39.             return response;  
  40.         }  
  41.     }  
  42. }  
Description
  • CRUDEntities-> This is our entity name
  • GetEmployees -> To load the data from database using entity framework
  • HttpResponseMessage-> To return the response to application about service response.
Configure Kendo UI with ASP.Net MVC Application

Here we going to enable the Kendo UI feature with our application. We have two way to enable Kendo UI feature in our application 
Way 1

Step 1 - Add Reference


Step 2

Install FortuneLab.Kendo.Mvc from NuGet

 

Once you installed the Kendo.MVC into your application, you can see the below screen that the kendo.MVC is added to your reference and predefined CSS and script files are added to your project under content and script folder. 

 

(OR) Way 2

Another best way you can enable Kendo UI by adding following script and CSS to a header in view

Call Kendo Scripts and CSS

To access predefined functionality from Kendo UI, we need to call the following scripts and CSS in our application. So, just add the below references in the head tag.
  1. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />  
  2. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />  
  3. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />  
  4. <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  5. <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
Create Controller with View

Here, I just created an empty controller and I named it as CRUD
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace CRUD_Application.Controllers {  
  7.     public class CrudController: Controller {  
  8.         // GET: Crud  
  9.         public ActionResult Index() {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  
Now add a view for this action method and add the below code to it.
  1. @using(Html.BeginForm()) { < div id = "EmployeeGrid" > < /div>  
  2. }  
Now add  the below script to load the database to kendo grid from Web API.
  1. <script type="text/javascript">  
  2.     $(document).ready(function() {  
  3.         var ApiUrl = "http://localhost:50537/";  
  4.         dataSource = new kendo.data.DataSource({  
  5.             transport: {  
  6.                 read: {  
  7.                     url: ApiUrl + "api/GetEmployees",  
  8.                     dataType: "json",  
  9.                 }  
  10.             },  
  11.         });  
  12.         $("#EmployeeGrid").kendoGrid({  
  13.             dataSource: dataSource,  
  14.             groupable: true,  
  15.             filterable: true,  
  16.             sortable: true,  
  17.             pageable: {  
  18.                 refresh: true,  
  19.                 pageSizes: true,  
  20.                 buttonCount: 5  
  21.             },  
  22.             columns: [{  
  23.                 field: "ID",  
  24.                 title: "Employee ID",  
  25.                 width: 100,  
  26.                 filterable: false,  
  27.             }, {  
  28.                 field: "Name",  
  29.                 title: "Name",  
  30.             }, {  
  31.                 field: "Designation",  
  32.                 title: "Designation",  
  33.                 filterable: {  
  34.                     multi: true,  
  35.                     search: true,  
  36.                     search: true  
  37.                 }  
  38.             }, {  
  39.                 field: "Department",  
  40.                 title: "Department",  
  41.                 filterable: {  
  42.                     multi: true,  
  43.                     search: true,  
  44.                     search: true  
  45.                 }  
  46.             }, {  
  47.                 field: "YOE",  
  48.                 title: "YOE",  
  49.                 width: 80,  
  50.                 filterable: {  
  51.                     multi: true  
  52.                 }  
  53.             }, {  
  54.                 field: "Status",  
  55.                 title: "Status"  
  56.             }, ],  
  57.             height: "500px",  
  58.             pageable: {  
  59.                 refresh: true,  
  60.                 pageSizes: true,  
  61.                 buttonCount: 5  
  62.             },  
  63.         }).data("kendoGrid");  
  64.     });  
  65. </script>  
Detailed Description 
  • <div id="EmployeeGrid"></div> ->This is will act as kendo grid
  • var ApiUrl = "http://localhost:50537/"; -> It's represent API project url(Web API)
  • url: ApiUrl + "api/GetEmployees", -> Calling the Web API action method to load employees from database using entityframework
  • $("#EmployeeGrid").kendoGrid({ -> It's represent EmployeeGrid will act as kendo grid and all kendo grid functionality will applied to this div
  • dataSource: dataSource, -> binding all datas to grid datasource
  • transport: { read: -> It's enable us to bind data to kendo grid
  • and there has basic feature for groupable,filterable,sortable,
  • columns: [-> It's represent which are the fields wants to display in grid
Now refer all kendo related CSS and script to layout page to enable all kendo related functionality and UI to application
  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>CRUD Opeations</title> @Styles.Render("~/Content/css")  
  8.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>  
  9.     <link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" />  
  10.     <link href="~/Content/kendo/Kendo.custom.css" rel="stylesheet" />  
  11.     <link href="~/Content/kendo/kendo.dataviz.min.css" rel="stylesheet" />  
  12.     <link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" />  
  13.     <link href="~/Content/kendo/kendo.rtl.min.css" rel="stylesheet" />  
  14.     <script src="~/Scripts/kendo/kendo.all.min.js"></script>  
  15.     <script src="~/Scripts/kendo/kendo.aspnetmvc.min.js"></script>  
  16. </head>  
  17.   
  18. <body>  
  19.     <div class="navbar navbar-inverse navbar-fixed-top">  
  20.         <div class="container">  
  21.             <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  22. <span class="icon-bar"></span>  
  23. <span class="icon-bar"></span>  
  24. <span class="icon-bar"></span>  
  25. </button> @Html.ActionLink("Home""Index""Crud"new { area = "" }, new { @class = "navbar-brand" }) </div>  
  26.             <div class="navbar-collapse collapse">  
  27.                 <ul class="nav navbar-nav"> </ul>  
  28.             </div>  
  29.         </div>  
  30.     </div>  
  31.     <div class="container body-content"> @RenderBody()  
  32.         <hr /> </div>  
  33. </body>  
  34.   
  35. </html> 
Startup Project 

In this solution we have two ASP.Net MVC and ASP.NET WEB API projects, so we need to set which project needs to execute first but here we need to execute both applications because then only we will get the data from a database through Web API. Now we will configure both projects as startup project. For that right click on solution and select the project to set. 

 

Select Multiple Startup projects

 

Now Build and run your application. 

WEB-API Result

 
Application Result 


Yeah! the Kendo grid is successfully implemented in our ASP.NET MVC Application with ASP.NET WEB API. 

I did attach content and Script folders with Kendo.MVC.dll and Views(which contains layout and index page) for your reference. For more Kendo UI grid configuration see Here.

Summary

In this article, you learned to bind the data to kendo grid in ASP.NET MVC using WEB API and Entity Framework.
I hope you enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome. In the next article, we will see about CRUD operation using Kendo UI grid in ASP.NET MVC with WEB API and Entity Framework.