In my previous article I discussed about create CRUD operation using Scaffolding with ASP.NET MVC 5 and database first approach using ASP.NET MVC 5 and Entity Framework 6 and model first approach using ASP.NET MVC 5 and Entity Framework 6
In this article I will discuss how to create dynamic JSON data from controller and bind server side data to the JQGrid.
ASP.NET MVC does not have any built in data binding controls like GridView, DetailsView etc. JQGrid provides similar functionality like other data binding controls provides like sorting, paging, ordering, column formatting etc. JQGrid makes developers life easy and saves there time rather he has to write code to display data in tabular manner.
So let's open Visual Studio and start creating new empty ASP.NET MVC5 application. In this article I am using Visual Studio Express 2013 for Web but you can use Visual Studio 2013 as well.
Now start creating ASP.NET Web project in Visual Studio 2013.
using MVCJQGrid.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCJQGrid.Controllers
{
public class MVCJQGridController : Controller
{
MVCJQGridModel db = new MVCJQGridModel();
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult getEmployees()
{
var jsonData = new
{
total = 1,
page = 1,
records = db.Employees.ToList().Count,
rows = (
from emp in db.Employees.ToList()
select new
{
id = emp.id,
cell = new string[] {
emp.Name.ToString(),
emp.Designation.ToString(),
emp.Gender.ToString(),
emp.Salary.ToString(),
emp.City, emp.State,
emp.Zip.ToString()
}
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
}
}
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-ui-1.10.4.min.js"></script>
<script src="~/Scripts/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>
JQGrid also includes "colModel" array, where you need to specify actual model. Currently I am setting "false" for add, edit and delete functionality. You can see here I am using JQuery $(document).ready() function that will execute, once document is ready. The most important property is "url" where you need to specify controller and action method name. As given in below code snippet:
<script language="javascript">
$(document).ready(function () {
$("#list2").jqGrid({
url: 'https://localhost:49765/MVCJQGrid/getEmployees',
datatype: "json",
contentType: "application/json; charset-utf-8",
mtype: 'GET',
colNames: ['ID', 'Employee Name', 'Designation',
'Gender', 'Salary', 'City', 'State', 'Zip'],
colModel: [
{ name: 'id', index: 'id', width: 55, sorttype: "int" },
{ name: 'Name', index: 'Name', width: 90 },
{ name: 'Designation', index: 'Designation', width: 100 },
{ name: 'Gender', index: 'Gender', width: 80 },
{ name: 'Salary', index: 'Salary', width: 80,
align: "right", sorttype: "float" },
{ name: 'City', index: 'City', width: 80 },
{ name: 'State', index: 'State', width: 150 },
{ name: 'Zip', index: 'Zip', width: 150 }
],
rowNum: 5,
rowList: [5, 10, 15],
pager: '#pager2',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
caption: "JSON Example"
});
jQuery("#list2").jqGrid('navGrid', '#pager2',
{ edit: false, add: false, del: false });
});
</script>
I hope this article is useful for you. I look forward for your comments and feedback. So please provide your valuable feedback so that I can make this blog better. You can also share this article by hitting below button.
Happy learning...