In this help topic we are going to show you how to build CRUD application using jqxGrid and ASP.NET MVC 3.
If you haven't already installed ASP.NET MVC 3 use this resource: http://www.asp.net/mvc/MVC 3
For this help topic you're also going to need the Entity Framework: http://www.microsoft.com/download/en/details.aspx?id=18504
For our purpose we will use the Northwind database which you can download from here. So let's begin!
Create new ASP.NET MVC 3 project and choose the "Empty project" option for template. For "View engine" select "Razor".
<!DOCTYPE html>jqxGrid is using few images. For best look of the widget we recommend you to add them into the project. You can do this as dragging the images folder (located in the folder containing the CSS files included in the previous section) and dropping it over the "Content" folder.<html><head><meta charset="utf-8" /><title>@ViewBag.Title</title><link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /><link href="@Url.Content("~/Content/jqx.base.css")" rel="stylesheet" type="text/css" /><link href="@Url.Content("~/Content/jqx.classic.css")" rel="stylesheet" type="text/css" /><script src="@Url.Content("~/Scripts/jquery-1.11.1.min.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxcore.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxbuttons.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxdata.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxgrid.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxgrid.edit.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxgrid.selection.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxmenu.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxscrollbar.js")" type="text/javascript"></script></head><body>@RenderBody()</body></html>
public JsonResult GetEmployees(){var dbResult = db.Employees.ToList();var employees = from e in dbResultselect new { e.EmployeeID, e.FirstName, e.LastName, e.Title, e.Address, e.City, e.Country };return Json(employees, JsonRequestBehavior.AllowGet);}public bool Update(Employee employee){if (ModelState.IsValid){db.Entry(employee).State = EntityState.Modified;db.SaveChanges();return true;}return false;}public void Delete(int EmployeeID){try{var employee = db.Employees.Find(EmployeeID);db.Employees.Remove(employee);db.SaveChanges();}catch (Exception){}}public bool Add(Employee employee){if (ModelState.IsValid){db.Employees.Add(employee);db.SaveChanges();return true;}return false;}
<script type="text/javascript">$(document).ready(function () {// prepare the datavar data = {};var theme = 'classic';var firstNames = ["Nancy", "Andrew","Janet", "Margaret", "Steven","Michael", "Robert", "Laura", "Anne"];var lastNames = ["Davolio", "Fuller","Leverling", "Peacock", "Buchanan","Suyama", "King", "Callahan", "Dodsworth"];var titles = ["Sales Representative","Vice President, Sales", "Sales Representative","Sales Representative", "Sales Manager","Sales Representative", "Sales Representative","Inside Sales Coordinator", "Sales Representative"];var address = ["507 - 20th Ave. E. Apt. 2A","908 W. Capital Way", "722 Moss Bay Blvd.","4110 Old Redmond Rd.", "14 Garrett Hill", "Coventry House","Miner Rd.", "Edgeham Hollow", "Winchester Way","4726 - 11th Ave. N.E.", "7 Houndstooth Rd."];var city = ["Seattle", "Tacoma", "Kirkland", "Redmond","London", "London", "London", "Seattle", "London"];var country = ["USA", "USA", "USA", "USA","UK", "UK", "UK", "USA", "UK"];var generaterow = function (id) {var row = {};var firtnameindex = Math.floor(Math.random() * firstNames.length);var lastnameindex = Math.floor(Math.random() * lastNames.length);var k = firtnameindex;row["EmployeeID"] = id;row["FirstName"] = firstNames[firtnameindex];row["LastName"] = lastNames[lastnameindex];row["Title"] = titles[k];row["Address"] = address[k];row["City"] = city[k];row["Country"] = country[k];row["Notes"] = row["FirstName"] +' received a BA in computer science from ' +'the University of Washington';return row;}var source ={datatype: "json",datafields: [{ name: 'EmployeeID' },{ name: 'FirstName' },{ name: 'LastName' },{ name: 'Title' },{ name: 'Address' },{ name: 'City' },{ name: 'Country' },{ name: 'Notes' }],id: 'EmployeeID',url: 'Employees/GetEmployees',addrow: function (rowid, rowdata) {// synchronize with the server - send insert commandvar data = $.param(rowdata);$.ajax({dataType: 'json',url: 'Employees/Add',data: data,success: function (data, status, xhr) {// insert command is executed.}});},deleterow: function (rowid) {// synchronize with the server - send delete commandvar data = "EmployeeID=" + rowid;$.ajax({dataType: 'json',url: 'Employees/Delete',data: data,success: function (data, status, xhr) {// delete command is executed.}});},updaterow: function (rowid, rowdata) {// synchronize with the server - send update commandvar data = $.param(rowdata);$.ajax({dataType: 'json',url: 'Employees/Update',data: data,success: function (data, status, xhr) {// update command is executed.}});}};// initialize jqxGrid$("#jqxgrid").jqxGrid({width: 500,height: 350,source: source,theme: theme,columns: [{ text: 'EmployeeID', datafield: 'EmployeeID', width: 100 },{ text: 'First Name', datafield: 'FirstName', width: 100 },{ text: 'Last Name', datafield: 'LastName', width: 100 },{ text: 'Title', datafield: 'Title', width: 180 },{ text: 'Address', datafield: 'Address', width: 180 },{ text: 'City', datafield: 'City', width: 100 },{ text: 'Country', datafield: 'Country', width: 140 }]});$("#addrowbutton").jqxButton({ theme: theme });$("#deleterowbutton").jqxButton({ theme: theme });$("#updaterowbutton").jqxButton({ theme: theme });// update row.$("#updaterowbutton").bind('click', function () {var datarow = generaterow();var selectedrowindex = $("#jqxgrid").jqxGrid('getselectedrowindex');var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;if (selectedrowindex >= 0 && selectedrowindex < rowscount) {var id = $("#jqxgrid").jqxGrid('getrowid', selectedrowindex);$("#jqxgrid").jqxGrid('updaterow', id, datarow);}});// create new row.$("#addrowbutton").bind('click', function () {var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;var datarow = generaterow(rowscount + 1);$("#jqxgrid").jqxGrid('addrow', null, datarow);});// delete row.$("#deleterowbutton").bind('click', function () {var selectedrowindex = $("#jqxgrid").jqxGrid('getselectedrowindex');var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;if (selectedrowindex >= 0 && selectedrowindex < rowscount) {var id = $("#jqxgrid").jqxGrid('getrowid', selectedrowindex);$("#jqxgrid").jqxGrid('deleterow', id);}});});</script>@{ViewBag.Title = "Index";}<h2>Index</h2><div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;"><div style="float: left;" id="jqxgrid"></div><div style="margin-left: 30px; float: left;"><div><input id="addrowbutton" type="button" value="Add New Row" /></div><div style="margin-top: 10px;"><input id="deleterowbutton" type="button" value="Delete Selected Row" /></div><div style="margin-top: 10px;"><input id="updaterowbutton" type="button" value="Update Selected Row" /></div></div></div>
public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute("Employees", // Route name"{controller}/{action}/{id}", // URL with parametersnew { controller = "Employees", action = "Index", id = UrlParameter.Optional } // Parameter defaults);}