JqGrid In An ASP.NET MVC Application Using Entity Framework

Showing data in a grid is a basic need of any application. For better user experience, we need sorting, paging, filters, and search functionality in the grid. jqGrid is a very lightweight client side grid, where we can get a lot of functionality easily.

Let us see how we will create a GqGrid and show data in ASP.NET MVC application.

Create a database named Student and create a Table named StudentInfo, as shown below:

Script is shown below to create the table,
  1. USE [Student]  
  2. GO  
  3.   
  4.   
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. CREATE TABLE [dbo].[StudentInfo](  
  12.     [StudentId] [intNOT NULL,  
  13.     [Name] [nvarchar](50) NULL,  
  14.     [Address] [nvarchar](50) NULL,  
  15.     [City] [nvarchar](50) NULL,  
  16.     [State] [nvarchar](50) NULL,  
  17.     [Country] [nchar](10) NULL,  
  18.     [Department] [nchar](10) NULL,  
  19.     [Marks] [intNULL,  
  20.  CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED   
  21. (  
  22.     [StudentId] ASC  
  23. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  24. ON [PRIMARY]  
  25.   
  26. GO  
Insert some records to the StudentInfo table, as shown below:

 
To fetch the data, we will create a simple stored procedure named “GetStudentInfo“, as shown below:
  1. USE [Student]  
  2. GO  
  3.   
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8.   
  9. CREATE  PROCEDURE [dbo].[GetStudentInfo]  
  10.       
  11. AS  
  12. BEGIN  
  13.       
  14.     SET NOCOUNT ON;  
  15.     
  16.     SELECT  StudentId, Name, Address, City, State,  
  17.      Country, Department, Marks from StudentInfo  
  18. END  

For test purposes, execute the stored procedure and you will get the output, as shown below:

 
Now our database is ready to use.

Now we will create a simple ASP.NET MVC Web Application. Go to Visual Studio and create a new project. 



Select MVC and click OK.



Now our project is created.

In the next step, we will use the Entity framework, which will call the above stored procedure to retrieve the data from the StudentInfo table.

For using Entity framework, we will create an Edmx, as shown below. Go to the project, add new Item, go to the data section and select “ADO.NET Entity Data Model”.

 
In the next step, select Generate from database.

 

 

Now, we will be able to see student entities that are created.



In the next step, we will get the jqGrid related file from the Nuget.

Download the Jquery.JQGrid from the Nuget package, as shown below:

 


We will get the required scripts for JqGrid from Nuget.

Now, we know that we have HomeController and we have the Index action, which returns the Index view.

We will call a different action, when the Index view is loaded.
  1. public ActionResult Index()  
  2.         {  
  3.   
  4.             return View();  
  5.         }  

We have the different action- GetJqGridData, which gets the data using Entity framework .The action method code is shown below:

  1. public ActionResult GetJqGridData()  
  2.     {  
  3.   
  4.         StudentEntities s = new StudentEntities();  
  5.         var studenlist = s.GetStudentInfo();  
  6.         var jsonData = new  
  7.         {  
  8.   
  9.             rows = studenlist  
  10.         };  
  11.         return Json(jsonData, JsonRequestBehavior.AllowGet);  
  12.   
  13.     }  

We will be calling by the index action. In the Index view, we have the studentGrid HTML table and studentGridPager as pager in a div and the required scripts for JqGrid.

Index.cshtml

  1. <div>  
  2.     <table id="studentGrid"></table>  
  3.     <div id="studentGridPager"></div>   
  4. </div>  
  5. @section scripts{  
  6.     <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />  
  7.     <link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />  
  8.     <script src="~/Scripts/jquery-ui-1.10.4.js"></script>  
  9.     <script src="~/Scripts/i18n/grid.locale-en.js"></script>  
  10.     <script src="~/Scripts/jquery.jqGrid.min.js"></script>  
  11.     <script src="~/Scripts/script.js"></script>  
  12. }  

When the script.js loads, it has the code to call the action GetJqGridData, gets the data, binds the columns and fills the data.

Script.js

  1. $(function ()
  2. {  
  3.     $("#studentGrid").jqGrid
  4.    ({  
  5.         url: "/Home/GetJqGridData",  
  6.         datatype: 'json',  
  7.         mtype: 'Get',  
  8.         colNames: ['Enrollment No', 'Name','Address', 'City' , 'State', 'Country' ,'Department'],  
  9.         colModel: [  
  10.             { key: true, name: 'StudentId', index: 'StudentId',  sortable: true },  
  11.             { key: false, name: 'Name', index: 'Name',  sortable: true },  
  12.   
  13.             { key: false, name: 'Address', index: 'Address', sortable: true },  
  14.             { key: false, name: 'City', index: 'City', editable: true },  
  15.             { key: false, name: 'State', index: 'State', editable: true },  
  16.   
  17.                  { key: false, name: 'Country', index: 'Country', editable: true },  
  18.             { key: false, name: 'Department', index: 'Department', editable: true }  
  19.   
  20.             ],  
  21.         pager: jQuery('#studentGridPager'),  
  22.         rowNum: 3,  
  23.         rowList: [10, 20, 30, 40, 50],  
  24.         height: '100%',  
  25.         viewrecords: true,  
  26.         caption: 'Students Records',  
  27.         emptyrecords: 'No Students Records are Available to Display',  
  28.         jsonReader: 
  29.         {  
  30.             root: "rows",  
  31.             page: "page",  
  32.             total: "total",  
  33.             records: "records",  
  34.             repeatitems: false,  
  35.             Id: "0"  
  36.         },  
  37.         autowidth: true,  
  38.         multiselect: false,  
  39.     }).navGrid('#studentGridPager', 
  40.    {  
  41.         edit: true, add: true, del: true, search: true,  
  42.         searchtext: "Search Student", refresh: true  
  43.     },  
  44.           
  45.         {  
  46.             zIndex: 100,  
  47.             caption: "Search Students",  
  48.             sopt: ['cn']  
  49.         });  
  50.   
  51. });  

Let us see some important point here.

 
 
The output is shown below when we run the page.



This is basic information on jqGrid. We can do sorting, paging, filtering of the data in it. We can easily add steps to add, modify or delete the records. We can use custom formatters to add different input controls in it. All the events are there to do event handling. Some of the event handling can be found here.

I hope you understood the basic information on JqGrid. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all