博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery easyui datagrid mvc server端分页排序筛选的实现
阅读量:6320 次
发布时间:2019-06-22

本文共 4153 字,大约阅读时间需要 13 分钟。

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
(); } public int page { get; set; } public int rows { get; set; } public string sort { get; set; } public string order { get; set; } public IEnumerable
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
>(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
>(filterRules); filter.filterRules = rules; } return filter; } } else { return BindModel(controllerContext, bindingContext); } } private Nullable
TryGet
(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
)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
< filterRule>>(filterRules); //int page = 1; int rows = 10; string sort = "Id"; string order = "asc"; ; IEnumerable
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代码

  

 

转载于:https://www.cnblogs.com/neozhu/p/4695257.html

你可能感兴趣的文章
Android IOS WebRTC 音视频开发总结(十九)-- kurento
查看>>
reshape2 数据操作 数据融合( cast)
查看>>
第一章、关于SQL Server数据库的备份和还原(sp_addumpdevice、backup、Restore)
查看>>
八、主从复制
查看>>
深入理解RunLoop 整理资料
查看>>
俄罗斯方块
查看>>
LabVIEW将字符串转化为十进制
查看>>
欢迎光临
查看>>
20131205
查看>>
数组相减
查看>>
【poj 1962】Corporative Network(图论--带权并查集 模版题)
查看>>
使用AngularJS学习MVC的基础知识分享
查看>>
UDP template 代码
查看>>
爬虫 高性能
查看>>
使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(二)
查看>>
干吧跌!~brothers!~~
查看>>
linux 0.11 源码学习(七)
查看>>
函数模板的简单用法
查看>>
利用 LINQ的skip和Take 方法对List实现分页效果
查看>>
python 中的列表解析和生成表达式 - 转
查看>>