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

本文共 4103 字,大约阅读时间需要 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代码

  

 

转载地址:http://bvzwo.baihongyu.com/

你可能感兴趣的文章
Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
查看>>
各大浏览器CSS Hack收集
查看>>
再谈 $* 和 $@ 在 Bash 中的表现
查看>>
Apache Commons工具集简介
查看>>
【翻译】Nginx的反向代理
查看>>
htm、html、shtml网页区别
查看>>
SpringCloud学习笔记:服务注册与发现Eureka(2)
查看>>
学习新 api 的思考过程 4.18
查看>>
想要设计自己的微服务?看这篇文章就对了
查看>>
[译] 原生 JavaScript 值得学习吗?答案是肯定的
查看>>
29岁了还一事无成是人生的常态?
查看>>
gRPC-rs:从 C 到 Rust
查看>>
Mysql-高性能索引
查看>>
chrome浏览器最小字号解决方案
查看>>
富文本编译器UEditor+SSM的使用
查看>>
Java EE之旅02 CSS基础
查看>>
kubernetes学习笔记 (二):k8s初体验
查看>>
swift3 0 流控制
查看>>
Data-Mediator专题之属性回调
查看>>
每天一个Linux命令之ps-查看系统进程信息
查看>>