In this help topic we are going to introduce you how to make server side filtering with 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.filter.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><script src="@Url.Content("~/Scripts/jqxlistbox.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jqxdropdownlist.js")" type="text/javascript"></script></head><body>@RenderBody()</body></html>
public JsonResult GetOrders(){var query = Request.QueryString;var dbResult = db.Database.SqlQuery<Order>(this.BuildQuery(query));var orders = from order in dbResultselect new Order{ShippedDate = order.ShippedDate,ShipName = order.ShipName,ShipAddress = order.ShipAddress,ShipCity = order.ShipCity,ShipCountry = order.ShipCountry};return Json(orders, JsonRequestBehavior.AllowGet);}private string BuildQuery( System.Collections.Specialized.NameValueCollection query){var filtersCount = int.Parse(query.GetValues("filterscount")[0]);var queryString = @"SELECT * FROM Orders ";var tmpDataField = "";var tmpFilterOperator = "";var where = "";if (filtersCount > 0){where = " WHERE (";}for (var i = 0; i < filtersCount; i += 1){var filterValue = query.GetValues("filtervalue" + i)[0];var filterCondition = query.GetValues("filtercondition" + i)[0];var filterDataField = query.GetValues("filterdatafield" + i)[0];var filterOperator = query.GetValues("filteroperator" + i)[0];if (tmpDataField == ""){tmpDataField = filterDataField;}else if (tmpDataField != filterDataField){where += ") AND (";}else if (tmpDataField == filterDataField){if (tmpFilterOperator == ""){where += " AND ";}else{where += " OR ";}}// build the "WHERE" clause depending //on the filter's condition, value and datafield.where += this.GetFilterCondition(filterCondition, filterDataField, filterValue);if (i == filtersCount - 1){where += ")";}tmpFilterOperator = filterOperator;tmpDataField = filterDataField;}queryString += where;return queryString;}private string GetFilterCondition(string filterCondition, string filterDataField, string filterValue){switch (filterCondition){case "CONTAINS":return " " + filterDataField + " LIKE '%" + filterValue + "%'";case "DOES_NOT_CONTAIN":return " " + filterDataField + " NOT LIKE '%" + filterValue + "%'";case "EQUAL":return " " + filterDataField + " = '" + filterValue + "'";case "NOT_EQUAL":return " " + filterDataField + " <> '" + filterValue + "'";case "GREATER_THAN":return " " + filterDataField + " > '" + filterValue + "'";case "LESS_THAN":return " " + filterDataField + " < '" + filterValue + "'";case "GREATER_THAN_OR_EQUAL":return " " + filterDataField + " >= '" + filterValue + "'";case "LESS_THAN_OR_EQUAL":return " " + filterDataField + " <= '" + filterValue + "'";case "STARTS_WITH":return " " + filterDataField + " LIKE '" + filterValue + "%'";case "ENDS_WITH":return " " + filterDataField + " LIKE '%" + filterValue + "'";case "NOT_EMPTY":return " " + filterDataField + " <> ''";}return "";}
<script type="text/javascript">$(document).ready(function () {// prepare the datavar theme = 'classic';var source ={datatype: "json",datafields: [{ name: 'ShippedDate', type: 'date' },{ name: 'ShipName' },{ name: 'ShipAddress' },{ name: 'ShipCity' },{ name: 'ShipCountry' }],url: 'Orders/GetOrders',filter: function () {// update the grid and send a request to the server.$("#jqxgrid").jqxGrid('updatebounddata');}};// initialize jqxGrid$("#jqxgrid").jqxGrid({source: source,theme: theme,filterable: true,columns: [{ text: 'Shipped Date', datafield: 'ShippedDate', cellsformat: 'd', width: 200 },{ text: 'Ship Name', datafield: 'ShipName', width: 200 },{ text: 'Address', datafield: 'ShipAddress', width: 180 },{ text: 'City', datafield: 'ShipCity', width: 100 },{ text: 'Country', datafield: 'ShipCountry', width: 140 }]});});</script><h2>Index</h2><div id="jqxgrid"></div>
public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute("Orders", // Route name"{controller}/{action}/{id}", // URL with parametersnew { controller = "Orders", action = "Index", id = UrlParameter.Optional } // Parameter defaults);}