Remote Binding Kendo Grid In Partial View Using ASP.NET MVC And Entity Framework

Introduction

In this article, I’ll describe how to remote bind the kendo grid in Partial View using ASP.NET.MVC and Entity Framework. The application is developed basically using Entity Framework database first approach and MVC

Prerequisites

Basic knowledge in ASP.NET MVC, Entity Framework and kendo UI framework

Set up the Database

Open SSMS with new query window, create a new database, and a table for the demo purpose. Here, the script to create a database and table is given below:

  1. CREATE DATABASE Employee  
  2. CREATE TABLE Employee  
  3.   (  
  4.   EmployeeID INT IDENTITY(1,1) CONSTRAINT Pk_Emp_Id PRIMARY KEY,  
  5.   FirstName VARCHAR(20),  
  6.   LastName VARCHAR(20)  
  7.   )  

Insert some sample records, as given below:

  1. INSERT INTO Employee VALUES('Bob','Ross')  
  2.  INSERT INTO Employee VALUES('Pradeep','Raj')  
  3.  INSERT INTO Employee VALUES('Arun','Kumar')  
  4.  INSERT INTO Employee VALUES('Vasanth','Kumar')  

The table design is given below:


Create New MVC Project

Create a new empty ASP.NET MVC application as per the following figures:

Open Visual Studio ->File ->New project ->ASP.NET Web Application.

 

Select MVC and click OK.

 

Please refer to my previous article to check out how to configure Kendo UI in ASP.NET MVC Application. 

Generate the Model

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

   

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

  
 
Click Next. 

Step 3: Click the New Connection button. The connection properties window will open.

 

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.

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



Step 6: Select Table to generate models for Employee table and click Finish.



Create 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 EmployeeController.

Write the following code in the Controller.

  1. private EmployeeEntities db = new EmployeeEntities();  
  2.    
  3. blic ActionResult Demo()  
  4.     {  
  5.           
  6.         return PartialView("_EmployeeList");  
  7.   
  8.     }        
  9.         
  10.     // GET: Employee  
  11.    public ActionResult GetAllEmployee([DataSourceRequest]DataSourceRequest request)  
  12.     {  
  13.         try {   
  14.   
  15.         var employee = db.Employees.ToList();  
  16.   
  17.        return Json(employee.ToDataSourceResult(request),JsonRequestBehavior.AllowGet);  
  18.         }  
  19.         catch(Exception ex)  
  20.         {  
  21.             return Json(ex.Message);  
  22.         }  
  23.   
  24.     }  
Creating a Partial View

Right click on View->Shared folder-> Add empty MVC View. In my case, I named it as _EmployeeList.cshtml,

 

Write the following code in it.

_EmployeeList.cshtml 
  1. @model KendoMvcApp.Models.Employee  
  2.   
  3.   
  4. <h2>Employee</h2>  
  5. <div class="container">  
  6.     <div class="row">  
  7.   
  8.         @(Html.Kendo().Grid<KendoMvcApp.Models.Employee>()  
  9.     .Name("EmpGrid")  
  10.     .Selectable()  
  11.     .Columns(columns =>  
  12.     {  
  13.         columns.Bound(c => c.EmployeeID);  
  14.         columns.Bound(c => c.FirstName);  
  15.         columns.Bound(c => c.LastName);  
  16.   
  17.     })  
  18.     .DataSource(dataSource => dataSource  
  19.         .Ajax()  
  20.         .Read(read => read.Action("GetAllEmployee", "Employee"))  
  21.         )  
  22.         )  
  23. </div>  
  24.     </div>  

Creating a View

Right click on View->Employee folder-> Add new MVC View, In my case, I named it as Index.cshtml

Write the following code in it.

Index.cshtml
  1. @model KendoMvcApp.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5.       
  6. }  
  7.   
  8. <div class="row">  
  9.     <button id="open" class="k-button">Open Employee Grid</button>  
  10. </div>  
  11.   
  12. <div class="container">  
  13.     <div class="row">  
  14.   
  15.        <br />  
  16.         @(Html.Kendo().Window()  
  17.         .Name("window")  
  18.         .Width(630)  
  19.         .Height(315)  
  20.         .Draggable()  
  21.         .Resizable()  
  22.         .Visible(false)  
  23.         .Title("Employee List Grid")  
  24.         .LoadContentFrom("Demo", "Employee")  
  25.         )  
  26.           
  27.          
  28.     </div>  
  29. </div>  
  30. <script type="text/javascript">  
  31.     $(document).ready(function () {  
  32.         debugger;  
  33.         var myWindow = $("#window");  
  34.         $("#open").click(function (e) {  
  35.             myWindow.data("kendoWindow").open();  
  36.         });         
  37.     });  
  38. </script>  

In this view, we have the button and the kendo window which is not visible initially. When the button click happens, the kendo window will open with the content of employee records in kendo grid.

Open _layout.cshtml and add following action link in it.

  1. <li>@Html.ActionLink("Employees","Index","Employee")</li>  
Result
 
 
 

Download Source Code.

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


Similar Articles