标签:
1自定义一个ModelBinder
public class filterRule { public string field { get; set; } public string op { get; set; } public string value { get; set; } } public class dataGridFilter { public dataGridFilter() { filterRules = new HashSet<filterRule>(); } public int page { get; set; } public int rows { get; set; } public string sort { get; set; } public string order { get; set; } public IEnumerable<filterRule> filterRules { get; set; } } public class DataGridFilterBinderProvider : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null) throw new ArgumentNullException("controllerContext", "controllerContext is null."); if (bindingContext == null) throw new ArgumentNullException("bindingContext", "bindingContext is null."); if (bindingContext.ModelType == typeof(dataGridFilter)) { HttpRequestBase request = controllerContext.HttpContext.Request; dataGridFilter filter = new dataGridFilter(); if (request.HttpMethod == "GET") { filter.order = request.QueryString["order"]; filter.page = Convert.ToInt32(request.QueryString["page"]); filter.rows = Convert.ToInt32(request.QueryString["rows"]); filter.sort = request.QueryString["sort"]; var filterRules = request.QueryString["filterRules"]; if (!string.IsNullOrEmpty(filterRules)) { var rules = JsonConvert.DeserializeObject<IEnumerable<filterRule>>(filterRules); filter.filterRules = rules; } return filter; } else { filter.order = request.Form["order"]; filter.page = Convert.ToInt32(request.Form["page"]); filter.rows = Convert.ToInt32(request.Form["rows"]); filter.sort = request.Form["sort"]; var filterRules = request.Form["filterRules"]; if (!string.IsNullOrEmpty(filterRules)) { var rules = JsonConvert.DeserializeObject<IEnumerable<filterRule>>(filterRules); filter.filterRules = rules; } return filter; } } else { return BindModel(controllerContext, bindingContext); } } private Nullable<T> TryGet<T>(ModelBindingContext bindingContext, string key) where T : struct { if (String.IsNullOrEmpty(key)) return null; ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key); if (valueResult == null && bindingContext.FallbackToEmptyPrefix == true) valueResult = bindingContext.ValueProvider.GetValue(key); bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueResult); if (valueResult == null) return null; try { return (Nullable<T>)valueResult.ConvertTo(typeof(T)); } catch (Exception ex) { bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex); return null; } } }
controller代码
public ActionResult Get([ModelBinder(typeof(DataGridFilterBinderProvider))]dataGridFilter request) { //var filter = JsonConvert.DeserializeObject<IEnumerable< filterRule>>(filterRules); //int page = 1; int rows = 10; string sort = "Id"; string order = "asc"; ; IEnumerable<filterRules> filterRules = null; int count = products.Count(); //var query = products.OrderBy(string.Format("{0} {1}", sort, order)).Skip((page-1)*rows).Take(rows); var query = products.OrderBy(string.Format("{0} {1}", request.sort, request.order)).Skip((request.page - 1) * request.rows).Take(request.rows); return Json(new { total = count, rows = query }, JsonRequestBehavior.AllowGet); }
html代码
<script type="text/javascript"> $(function () { $(‘#grid‘).datagrid({ title: ‘Product--100‘, url: ‘/Home/Get‘, method:‘post‘, width: ‘100%‘, height: ‘auto‘, rownumbers: true, sortName: ‘Id‘, SortOrder: ‘asc‘, columns: [[ { field: ‘Id‘, title: ‘Id‘, sortable: true }, { field: ‘Name‘, title: ‘Name‘, sortable: true }, { field: ‘Unit‘, title: ‘Unit‘, sortable: true }, { field: ‘Price‘, title: ‘Price‘, sortable: true } ]], pagination: true, pagePosition: ‘bottom‘, pageSize: 10, enableFilter:true, remoteFilter:true }).datagrid(‘enableFilter‘); }) </script>
jquery easyui datagrid mvc server端分页排序筛选的实现
标签:
原文地址:http://www.cnblogs.com/neozhu/p/4695257.html