In this tutorial we are going to introduce you how to use jqxGrid in ASP.NET MVC 4.
If you haven't already installed ASP.NET MVC 4 use this resource: http://www.asp.net/mvc/mvc4
For this tutorial you're also going to need the Entity Framework: http://www.microsoft.com/download/en/details.aspx?id=18504
The database which we will use is the AdventureWorks database which you can download from here. So let's begin!
Create a new ASP.NET MVC 4 project. For "View engine" select "Razor".
using System;using System.Collections.Generic;using System.Data;using System.Data.Entity;using System.Linq;using System.Web;using System.Web.Mvc;namespace Grid_MVC4.Controllers{public class EmployeeController : Controller{private Model1Container db = new Model1Container();public JsonResult GetEmployees(){var dbResult = db.Employees.ToList();var employees = (from employee in dbResultselect new{employee.BirthDate,employee.Gender,employee.JobTitle,employee.SickLeaveHours,employee.VacationHours});return Json(employees, JsonRequestBehavior.AllowGet);}//// GET: /Employee/public ActionResult Index(){return View(db.Employees.ToList());}//// GET: /Employee/Details/5public ActionResult Details(int id = 0){Employee employee = db.Employees.Find(id);if (employee == null){return HttpNotFound();}return View(employee);}//// GET: /Employee/Createpublic ActionResult Create(){return View();}//// POST: /Employee/Create[HttpPost]public ActionResult Create(Employee employee){if (ModelState.IsValid){db.Employees.Add(employee);db.SaveChanges();return RedirectToAction("Index");}return View(employee);}//// GET: /Employee/Edit/5public ActionResult Edit(int id = 0){Employee employee = db.Employees.Find(id);if (employee == null){return HttpNotFound();}return View(employee);}//// POST: /Employee/Edit/5[HttpPost]public ActionResult Edit(Employee employee){if (ModelState.IsValid){db.Entry(employee).State = EntityState.Modified;db.SaveChanges();return RedirectToAction("Index");}return View(employee);}//// GET: /Employee/Delete/5public ActionResult Delete(int id = 0){Employee employee = db.Employees.Find(id);if (employee == null){return HttpNotFound();}return View(employee);}//// POST: /Employee/Delete/5[HttpPost, ActionName("Delete")]public ActionResult DeleteConfirmed(int id){Employee employee = db.Employees.Find(id);db.Employees.Remove(employee);db.SaveChanges();return RedirectToAction("Index");}protected override void Dispose(bool disposing){db.Dispose();base.Dispose(disposing);}}}
@model IEnumerable<Grid_MVC4.Employee>@{ViewBag.Title = "Index";}@section scripts {<script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/scripts/jquery-1.11.1.min.js"></script><script type="text/javascript" src="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/jqx-all.js"></script><link rel="stylesheet" type="text/css" href="http://jqwidgets.com/jquery-widgets-demo/jqwidgets/styles/jqx.base.css" /><script type="text/javascript">$(document).ready(function () {// prepare the datavar source ={datatype: "json",datafields: [{ name: 'BirthDate', type: 'date' },{ name: 'Gender', type: 'string' },{ name: 'JobTitle', type: 'string' },{ name: 'SickLeaveHours', type: 'string' },{ name: 'VacationHours', type: 'string' }],url: 'Employee/GetEmployees'};var dataAdapter = new $.jqx.dataAdapter(source);// initialize jqxGrid$("#grid").jqxGrid({width: 600,source: dataAdapter,columns: [{ text: "Birth Date", datafield: "BirthDate", cellsformat: 'd' },{ text: "Gender", datafield: "Gender" },{ text: "Job Title", datafield: "JobTitle" },{ text: "Sick Leave Hours", datafield: "SickLeaveHours" },{ text: "Vacation Hours", datafield: "VacationHours" }]});});</script>}<h2>Index</h2><div id="grid"></div>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace Grid_MVC4{public class RouteConfig{public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute(name: "Employee",url: "{controller}/{action}/{id}",defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional });}}}