public ActionResult AjaxBinding(DataTablesParams dataTablesParams) { var logs = _logProvider.GetAllLogs(); var totalCount = logs.Count(); IQueryable<Log> q = logs; if (!string.IsNullOrEmpty(dataTablesParams.sSearch)) { q = q.Where(b => b.Logger.Contains(dataTablesParams.sSearch) || b.Exception.Contains(dataTablesParams.sSearch) || b.Message.Contains(dataTablesParams.sSearch)); } int filteredCount = q.Count(); IQueryable<Log> sorted = q; for (int i = 0; i < dataTablesParams.iSortingCols; i++) { int sortCol = dataTablesParams.iSortCol[i]; var sortColName = sortCol == 0 ? "Time" : sortCol == 1 ? "Level" : "Logger"; var sortExpression = String.Format("{0} {1}", sortColName, dataTablesParams.sSortDir[i]); sorted = sorted.OrderBy(sortExpression); } IQueryable<Log> filteredAndSorted = sorted; if (filteredCount > dataTablesParams.iDisplayLength) { filteredAndSorted = sorted.Skip(dataTablesParams.iDisplayStart).Take(dataTablesParams.iDisplayLength); } var logModels = filteredAndSorted.ToList().Select(s => new LogModel { Time = s.Time.ToString(), Level = s.Level, Source = s.Logger, Message = s.Message, Method = s.Method, ExceptionType = s.ExceptionType, Exception = s.Exception }); return Json(new { sEcho = dataTablesParams.sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = filteredCount, aaData = logModels }, JsonRequestBehavior.AllowGet); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { DataTablesParams obj = new DataTablesParams(); var request = controllerContext.HttpContext.Request.Params; obj.iDisplayStart = Convert.ToInt32(request["iDisplayStart"]); obj.iDisplayLength = Convert.ToInt32(request["iDisplayLength"]); obj.iColumns = Convert.ToInt32(request["iColumns"]); obj.sSearch = request["sSearch"]; obj.bEscapeRegex = Convert.ToBoolean(request["bEscapeRegex"]); obj.iSortingCols = Convert.ToInt32(request["iSortingCols"]); obj.sEcho = int.Parse(request["sEcho"]); for (int i = 0; i < obj.iColumns; i++) { obj.bSortable.Add(Convert.ToBoolean(request["bSortable_" + i])); obj.bSearchable.Add(Convert.ToBoolean(request["bSearchable_" + i])); obj.sSearchColumns.Add(request["sSearch_" + i]); obj.bEscapeRegexColumns.Add(Convert.ToBoolean(request["bEscapeRegex_" + i])); obj.iSortCol.Add(Convert.ToInt32(request["iSortCol_" + i])); obj.sSortDir.Add(request["sSortDir_" + i]); } return obj; }