ASP.NET MVC5 - Kendo UI Grid CRUD Operation Using ASP.NET Web API And EF

Introduction
 
 In this article, I will demonstrate how to perform CRUD operations in Kendo grid, using ASP.NET.MVC, ASP.NET WEB API, and Entity Framework.
 
Prerequisites 
  • Visual Studio
  • SQL Server
  • Basic knowledge of ASP.NET MVC
  • Basic knowledge of ASP.NET Web API
  • Basic knowledge of Entity Framework
  • Basic knowledge of Kendo UI
  • Basic knowledge of jQuery
  • Basic knowledge of CSS
Article Flow 
  • Create a table in database with dummy values
  • Create an ASP.NET Web API Empty project with CRUD Logics Using Entity Framework
  • Create an ASP.NET MVC Empty project
  • Create a Controller and View
  • Enable Kendo UI Features
 Create a table in database with dummy values
 
First, we will create a table in SQL Server to populate a Kendo UI grid 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. GO  
  2. CREATE TABLE [dbo].[Employee](  
  3. [ID] [bigint] IDENTITY(1,1) NOT NULL,  
  4. [FullName] [nvarchar](150) NULL,  
  5. [Designation] [nvarchar](50) NULL,  
  6. [Phone] [nvarchar](20) NULL,  
  7. [Email] [nvarchar](50) NULL,  
  8. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED  
  9. (  
  10. [ID] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13. 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], [FullName], [Designation], [Phone], [Email]) VALUES (1, N'Gnanavel', N'Software Engineer', N'9876543210', N'[email protected]')  
  5. GO  
  6. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (2, N'Robert ', N'Software Engineer', N'9632884100', N'[email protected]')  
  7. GO  
  8. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (3, N'Muthu ', N'Software Engineer', N'7894563210', N'[email protected]')  
  9. GO  
  10. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (4, N'Ramar ', N'Sr. Software Engineer', N'4561230789', N'[email protected]')  
  11. GO  
  12. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (5, N'Gokul', N'Sr. Software Engineer', N'7412589630', N'[email protected]')  
  13. GO  
  14. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (6, N'Karthick', N'Application Developer', N'8523697410', N'[email protected]')  
  15. GO  
  16. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (7, N'Sharma', N'Application Developer', N'9845632107', N'[email protected]')  
  17. GO  
  18. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (8, N'Sheriff', N'Application Developer', N'7563241890', N'[email protected]')  
  19. GO  
  20. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (9, N'Anand', N'Application Developer', N'9875163240', N'[email protected]')  
  21. GO  
  22. INSERT [dbo].[Employee] ([ID], [FullName], [Designation], [Phone], [Email]) VALUES (10, N'Mubarak', N'Application Developer', N'7123456890', N'[email protected]')  
  23. GO  
  24. SET IDENTITY_INSERT [dbo].[Employee] OFF  
  25. GO  
Create an ASP.NET Web API Empty project with CRUD Logics Using Entity Framework
 
I already discussed how to create an Empty ASP.NET Web API project and  Implement WEB API scaffolding with Entity Framework, Refer Implement ASP.NET WEB API CRUD Logic Using Entity Framework Without Writing Code And Test It With Postman. While creating the project I named it as "API".Entity Framework implements the database first approach and In Meantime choose our created table with entity framework and configure with SQL table "Employee" from CSharpCorner database and with our API. Once you do those steps your API will be like below,
 
 
 
and the controller will be,
  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. using System.Data;  
  11. namespace Web_API.Controllers {  
  12.     /// <summary>  
  13.     /// Crud Operation Controller  
  14.     /// </summary>  
  15.     public class CRUDController: ApiController {  
  16.         private static CRUDEntities crud = new CRUDEntities();  
  17.         /// <summary>  
  18.         /// Get All Employee Details  
  19.         /// </summary>  
  20.         /// <returns></returns>  
  21.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  22.         [System.Web.Http.Route("api/GetEmployees")]  
  23.         [System.Web.Http.HttpGet]  
  24.         public HttpResponseMessage GetEmployees() {  
  25.             var result = crud.Employees.ToList();  
  26.             return GetResultResponse(result);  
  27.         }  
  28.         /// <summary>  
  29.         /// Get Employee Details  
  30.         /// </summary>  
  31.         /// <remarks>  
  32.         /// Get Employee Details based on empid  
  33.         /// </remarks>  
  34.         /// <param name="employeeid"></param>  
  35.         /// <returns></returns>  
  36.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  37.         [System.Web.Http.Route("api/GetEmployee")]  
  38.         [System.Web.Http.HttpGet]  
  39.         public HttpResponseMessage GetEmployee(int employeeid) {  
  40.             var result = crud.Employees.Where(a => a.ID == employeeid).ToList();  
  41.             return GetResultResponse(result);  
  42.         }  
  43.         /// <summary>  
  44.         /// Add new employee  
  45.         /// </summary>  
  46.         /// <remarks>  
  47.         /// Create new employee and return inserted employee details  
  48.         /// </remarks>  
  49.         /// <param name="employee"></param>  
  50.         /// <returns></returns>  
  51.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  52.         [System.Web.Http.Route("api/AddEmployee")]  
  53.         [System.Web.Http.HttpPost]  
  54.         public HttpResponseMessage AddEmployee(Employee employee) {  
  55.             var result = crud.Employees.Add(employee);  
  56.             crud.SaveChanges();  
  57.             return GetResultResponse(result);  
  58.         }  
  59.         /// <summary>  
  60.         /// Update Employee Details  
  61.         /// </summary>  
  62.         /// <remarks>  
  63.         /// Update Employee Details Based on empid  
  64.         /// </remarks>  
  65.         /// <param name="employee"></param>  
  66.         /// <returns></returns>  
  67.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  68.         [System.Web.Http.Route("api/UpdateEmployee")]  
  69.         [System.Web.Http.HttpPut]  
  70.         public HttpResponseMessage UpdateEmployee(Employee employee) {  
  71.             Employee result = crud.Employees.Where(a => a.ID == employee.ID).FirstOrDefault();  
  72.             if (result != null) {  
  73.                 result.FullName = employee.FullName;  
  74.                 result.Designation = employee.Designation;  
  75.                 result.Phone = employee.Phone;  
  76.                 result.Designation = employee.Designation;  
  77.                 result.Email = employee.Email;  
  78.                 crud.Entry(result).State = EntityState.Modified;  
  79.                 crud.SaveChanges();  
  80.             }  
  81.             return GetResultResponse(result);  
  82.         }  
  83.         /// <summary>  
  84.         /// Delete Employee  
  85.         /// </summary>  
  86.         /// <remarks>  
  87.         /// Delete Employee record based on empid  
  88.         /// </remarks>  
  89.         /// <param name="employee"></param>  
  90.         /// <returns></returns>  
  91.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  92.         [System.Web.Http.Route("api/DeleteEmployee")]  
  93.         [System.Web.Http.HttpDelete]  
  94.         public void DeleteEmployee(Employee employee) {  
  95.             Employee result = crud.Employees.Where(a => a.ID == employee.ID).FirstOrDefault();  
  96.             if (result != null) {  
  97.                 crud.Entry(result).State = EntityState.Deleted;  
  98.                 crud.SaveChanges();  
  99.             }  
  100.         }  
  101.         /// <summary>  
  102.         /// Get Response for Each result  
  103.         /// </summary>  
  104.         /// <param name="Result"></param>  
  105.         /// <returns></returns>  
  106.         public HttpResponseMessage GetResultResponse(object Result) {  
  107.             HttpResponseMessage response = null;  
  108.             try {  
  109.                 response = Request.CreateResponse(HttpStatusCode.OK, Result);  
  110.             } catch (Exception ex) {  
  111.                 response = Request.CreateResponse(HttpStatusCode.BadRequest, Result);  
  112.             }  
  113.             return response;  
  114.         }  
  115.     }  
  116. }  
And you might get successful services

 
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.
  • Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it as "Application".
  • Now, click OK.
  • Then, select Empty MVC template and click OK to create the project.
  • Once you click OK, the project will be created with the basic architecture of MVC.
  • 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.
 
Create a Controller and View
 
Now, create an empty Controller and View. Here, I created a Controller with the name of "CrudController". 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. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>  
  2. <link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" />  
  3. <link href="~/Content/kendo/Kendo.custom.css" rel="stylesheet" />  
  4. <link href="~/Content/kendo/kendo.dataviz.min.css" rel="stylesheet" />  
  5. <link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" />  
  6. <link href="~/Content/kendo/kendo.rtl.min.css" rel="stylesheet" />  
  7. <script src="~/Scripts/kendo/kendo.all.min.js"></script>  
  8. <script src="~/Scripts/kendo/kendo.aspnetmvc.min.js"></script>  
Now create a div which will act as Kendo grid
  1. <div id="EmployeeGrid" style="width: 750px;"></div>   
And add the below code to your view to perform CRUD operations
  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.                 destroy: {  
  11.                     url: ApiUrl + "api/DeleteEmployee",  
  12.                     type: "DELETE"  
  13.                 },  
  14.                 create: {  
  15.                     url: ApiUrl + "api/AddEmployee",  
  16.                     type: "POST"  
  17.                 },  
  18.                 update: {  
  19.                     url: ApiUrl + "api/UpdateEmployee",  
  20.                     type: "PUT",  
  21.                     parameterMap: function(options, operation) {  
  22.                         if (operation !== "read" && options.models) {  
  23.                             return {  
  24.                                 models: kendo.stringify(options.models)  
  25.                             };  
  26.                         }  
  27.                     }  
  28.                 },  
  29.             },  
  30.             schema: {  
  31.                 model: {  
  32.                     id: "ID",  
  33.                     fields: {  
  34.                         FullName: {  
  35.                             editable: true,  
  36.                             nullable: false,  
  37.                             type: "string",  
  38.                             validation: {  
  39.                                 required: true  
  40.                             }  
  41.                         },  
  42.                         Designation: {  
  43.                             editable: true,  
  44.                             nullable: false,  
  45.                             type: "string",  
  46.                             validation: {  
  47.                                 required: true  
  48.                             }  
  49.                         },  
  50.                         Phone: {  
  51.                             editable: true,  
  52.                             nullable: false,  
  53.                             type: "string",  
  54.                             validation: {  
  55.                                 required: true  
  56.                             }  
  57.                         },  
  58.                         Email: {  
  59.                             editable: true,  
  60.                             nullable: false,  
  61.                             type: "string",  
  62.                             validation: {  
  63.                                 required: true  
  64.                             }  
  65.                         },  
  66.                     }  
  67.                 }  
  68.             }  
  69.         });  
  70.         $("#EmployeeGrid").kendoGrid({  
  71.             dataSource: dataSource,  
  72.             editable: "popup",  
  73.             groupable: true,  
  74.             filterable: true,  
  75.             sortable: true,  
  76.             pageable: {  
  77.                 refresh: true,  
  78.                 pageSizes: true,  
  79.                 buttonCount: 5  
  80.             },  
  81.             edit: function(e) {  
  82.                 e.container.find("input:first").hide();  
  83.             },  
  84.             toolbar: ["create"],  
  85.             columns: [{  
  86.                 field: "ID",  
  87.                 title: "ID",  
  88.                 width: 25,  
  89.                 filterable: false,  
  90.             }, {  
  91.                 field: "FullName",  
  92.                 title: "Name",  
  93.                 width: 120,  
  94.             }, {  
  95.                 field: "Designation",  
  96.                 title: "Designation",  
  97.                 filterable: {  
  98.                     multi: true,  
  99.                     search: true  
  100.                 },  
  101.                 width: 150,  
  102.             }, {  
  103.                 field: "Phone",  
  104.                 title: "Phone",  
  105.                 filterable: true,  
  106.                 width: 90,  
  107.             }, {  
  108.                 field: "Email",  
  109.                 title: "Email",  
  110.                 width: 150,  
  111.                 filterable: true  
  112.             }, {  
  113.                 command: ["edit", {  
  114.                     name: "destroy",  
  115.                     text: "remove",  
  116.                 }],  
  117.             }],  
  118.             height: "500px",  
  119.             pageable: {  
  120.                 refresh: true,  
  121.                 pageSizes: true,  
  122.                 buttonCount: 5  
  123.             },  
  124.         }).data("kendoGrid");  
  125.     });  
  126. </script>  
  127. <style>  
  128.     .k-window {  
  129.         width: 300px !important;  
  130.     }  
  131. </style>  
Detailed description 
  •  var ApiUrl = "http://localhost:50537/" -> it represents our Web API url
  • new kendo.data.DataSource( ->  it helps us to do remote data binding in Kendo Grid which is the response of the REST API developed using ASP.NET Web API using Entity Framework. The data source of the Kendo grid is constructed based on our complex JSON
  • Transport-> It helps us to do the operations Create, Read, Update and Delete
  • Transport Read-> It reads the data from respective API with the help of url; and dataType parameter represents what type of data we are reading from API
  •  Transport Destroy-> It's used to delete the respective record by calling respective API action method and type parameter represents what type of HTTP request we are passing to the API, here we are passing the Delete request.
  • Transport Create-> It helps us to create a new record database by calling respective API action method and Passing the model as a parameter to the respective action method and here the type will be Post.
  • Transport update-> It helps us to update the existing record in a database by calling respective API action method using url and passing the model as a parameter to the respective action method and here the type will be Put. 
  • schema model -> It will be the input parameter to the API and we will do the validation and represent the type of data from the user and in a model, id parameter represents the primary column field to skip editing and do tracking of the respective record.
  • $("#EmployeeGrid").kendoGrid({ dataSource:-> Here, we are binding the remote data to the kendo grid data source
  • editable: "popup",-> Here we will navigate to the popup during the addition and updating the record. One more option is to do the Add and Update the record in the grid itself using inline.
  • toolbar: ["create"], -> It enables the Add new record functionality to the Kendo grid on top of the grid in toolbar
  • columns:->  This parameter is represented which are the fields which need to be displayed in the grid
  • command: ->It helps us to enable the important command of edit, delete and cancel to the Kendo Grid.
Now run your application
 
 
Add New Record
 
Click Add new record button in the toolbar of a grid to add a new record in Popup edit mode. 
 
 
Edit Record 
 
Click Edit button in the respective row of a grid to update existing record in Popup mode. 
 
 
 
 Delete Record
 
Click Remove button in the respective row of a grid to delete existing record in Popup mode.
 
 
 
Application -Complete View
  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.                 destroy: {  
  11.                     url: ApiUrl + "api/DeleteEmployee",  
  12.                     type: "DELETE"  
  13.                 },  
  14.                 create: {  
  15.                     url: ApiUrl + "api/AddEmployee",  
  16.                     type: "POST"  
  17.                 },  
  18.                 update: {  
  19.                     url: ApiUrl + "api/UpdateEmployee",  
  20.                     type: "PUT",  
  21.                     parameterMap: function(options, operation) {  
  22.                         if (operation !== "read" && options.models) {  
  23.                             return {  
  24.                                 models: kendo.stringify(options.models)  
  25.                             };  
  26.                         }  
  27.                     }  
  28.                 },  
  29.             },  
  30.             schema: {  
  31.                 model: {  
  32.                     id: "ID",  
  33.                     fields: {  
  34.                         FullName: {  
  35.                             editable: true,  
  36.                             nullable: false,  
  37.                             type: "string",  
  38.                             validation: {  
  39.                                 required: true  
  40.                             }  
  41.                         },  
  42.                         Designation: {  
  43.                             editable: true,  
  44.                             nullable: false,  
  45.                             type: "string",  
  46.                             validation: {  
  47.                                 required: true  
  48.                             }  
  49.                         },  
  50.                         Phone: {  
  51.                             editable: true,  
  52.                             nullable: false,  
  53.                             type: "string",  
  54.                             validation: {  
  55.                                 required: true  
  56.                             }  
  57.                         },  
  58.                         Email: {  
  59.                             editable: true,  
  60.                             nullable: false,  
  61.                             type: "string",  
  62.                             validation: {  
  63.                                 required: true  
  64.                             }  
  65.                         },  
  66.                     }  
  67.                 }  
  68.             }  
  69.         });  
  70.         $("#EmployeeGrid").kendoGrid({  
  71.             dataSource: dataSource,  
  72.             editable: "popup",  
  73.             groupable: true,  
  74.             filterable: true,  
  75.             sortable: true,  
  76.             pageable: {  
  77.                 refresh: true,  
  78.                 pageSizes: true,  
  79.                 buttonCount: 5  
  80.             },  
  81.             edit: function(e) {  
  82.                 e.container.find("input:first").hide();  
  83.             },  
  84.             toolbar: ["create"],  
  85.             columns: [{  
  86.                 field: "ID",  
  87.                 title: "ID",  
  88.                 width: 25,  
  89.                 filterable: false,  
  90.             }, {  
  91.                 field: "FullName",  
  92.                 title: "Name",  
  93.                 width: 120,  
  94.             }, {  
  95.                 field: "Designation",  
  96.                 title: "Designation",  
  97.                 filterable: {  
  98.                     multi: true,  
  99.                     search: true  
  100.                 },  
  101.                 width: 150,  
  102.             }, {  
  103.                 field: "Phone",  
  104.                 title: "Phone",  
  105.                 filterable: true,  
  106.                 width: 90,  
  107.             }, {  
  108.                 field: "Email",  
  109.                 title: "Email",  
  110.                 width: 150,  
  111.                 filterable: true  
  112.             }, {  
  113.                 command: ["edit", {  
  114.                     name: "destroy",  
  115.                     text: "remove",  
  116.                 }],  
  117.             }],  
  118.             height: "500px",  
  119.             pageable: {  
  120.                 refresh: true,  
  121.                 pageSizes: true,  
  122.                 buttonCount: 5  
  123.             },  
  124.         }).data("kendoGrid");  
  125.     });  
  126. </script>  
  127. <style>  
  128.     .k-window {  
  129.         width: 300px !important;  
  130.     }  
  131. </style> @using (Html.BeginForm("Index""Crud", FormMethod.Post, new { id = "EmployeeDetails" })) {  
  132. <div id="EmployeeGrid" style="width: 750px;"></div> }  
_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>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(" ASP.NET MVC5 - Kendo Grid CRUD Operation using Web API AND Entity Framework""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>  
API -Complete Controller 
  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. using System.Data;  
  11. namespace Web_API.Controllers {  
  12.     /// <summary>  
  13.     /// Crud Operation Controller  
  14.     /// </summary>  
  15.     public class CRUDController: ApiController {  
  16.         private static CRUDEntities crud = new CRUDEntities();  
  17.         /// <summary>  
  18.         /// Get All Employee Details  
  19.         /// </summary>  
  20.         /// <returns></returns>  
  21.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  22.         [System.Web.Http.Route("api/GetEmployees")]  
  23.         [System.Web.Http.HttpGet]  
  24.         public HttpResponseMessage GetEmployees() {  
  25.             var result = crud.Employees.ToList();  
  26.             return GetResultResponse(result);  
  27.         }  
  28.         /// <summary>  
  29.         /// Get Employee Details  
  30.         /// </summary>  
  31.         /// <remarks>  
  32.         /// Get Employee Details based on empid  
  33.         /// </remarks>  
  34.         /// <param name="employeeid"></param>  
  35.         /// <returns></returns>  
  36.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  37.         [System.Web.Http.Route("api/GetEmployee")]  
  38.         [System.Web.Http.HttpGet]  
  39.         public HttpResponseMessage GetEmployee(int employeeid) {  
  40.             var result = crud.Employees.Where(a => a.ID == employeeid).ToList();  
  41.             return GetResultResponse(result);  
  42.         }  
  43.         /// <summary>  
  44.         /// Add new employee  
  45.         /// </summary>  
  46.         /// <remarks>  
  47.         /// Create new employee and return inserted employee details  
  48.         /// </remarks>  
  49.         /// <param name="employee"></param>  
  50.         /// <returns></returns>  
  51.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  52.         [System.Web.Http.Route("api/AddEmployee")]  
  53.         [System.Web.Http.HttpPost]  
  54.         public HttpResponseMessage AddEmployee(Employee employee) {  
  55.             var result = crud.Employees.Add(employee);  
  56.             crud.SaveChanges();  
  57.             return GetResultResponse(result);  
  58.         }  
  59.         /// <summary>  
  60.         /// Update Employee Details  
  61.         /// </summary>  
  62.         /// <remarks>  
  63.         /// Update Employee Details Based on empid  
  64.         /// </remarks>  
  65.         /// <param name="employee"></param>  
  66.         /// <returns></returns>  
  67.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  68.         [System.Web.Http.Route("api/UpdateEmployee")]  
  69.         [System.Web.Http.HttpPut]  
  70.         public HttpResponseMessage UpdateEmployee(Employee employee) {  
  71.             Employee result = crud.Employees.Where(a => a.ID == employee.ID).FirstOrDefault();  
  72.             if (result != null) {  
  73.                 result.FullName = employee.FullName;  
  74.                 result.Designation = employee.Designation;  
  75.                 result.Phone = employee.Phone;  
  76.                 result.Designation = employee.Designation;  
  77.                 result.Email = employee.Email;  
  78.                 crud.Entry(result).State = EntityState.Modified;  
  79.                 crud.SaveChanges();  
  80.             }  
  81.             return GetResultResponse(result);  
  82.         }  
  83.         /// <summary>  
  84.         /// Delete Employee  
  85.         /// </summary>  
  86.         /// <remarks>  
  87.         /// Delete Employee record based on empid  
  88.         /// </remarks>  
  89.         /// <param name="employee"></param>  
  90.         /// <returns></returns>  
  91.         [ResponseType(typeof(IEnumerable < Employee > ))]  
  92.         [System.Web.Http.Route("api/DeleteEmployee")]  
  93.         [System.Web.Http.HttpDelete]  
  94.         public void DeleteEmployee(Employee employee) {  
  95.             Employee result = crud.Employees.Where(a => a.ID == employee.ID).FirstOrDefault();  
  96.             if (result != null) {  
  97.                 crud.Entry(result).State = EntityState.Deleted;  
  98.                 crud.SaveChanges();  
  99.             }  
  100.         }  
  101.         /// <summary>  
  102.         /// Get Response for Each result  
  103.         /// </summary>  
  104.         /// <param name="Result"></param>  
  105.         /// <returns></returns>  
  106.         public HttpResponseMessage GetResultResponse(object Result) {  
  107.             HttpResponseMessage response = null;  
  108.             try {  
  109.                 response = Request.CreateResponse(HttpStatusCode.OK, Result);  
  110.             } catch (Exception ex) {  
  111.                 response = Request.CreateResponse(HttpStatusCode.BadRequest, Result);  
  112.             }  
  113.             return response;  
  114.         }  
  115.     }  
  116. }  
I attached the demonstrated Application and API without the packages due to size limit. In the next article, we will see Kendo Grid and more features of CRUD in the next article.
 
Summary 

In this article, we discussed how to do the CRUD operation in Kendo UI Grid using ASP.NET Web API and Entity Framework in ASP.NET MVC5 Web Application.
 
I Hope it will help you. Your valuable feedback and comments about this article are always welcome.  


Similar Articles