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代码