Exemplo n.º 1
0
        public JsonResult DataBind(IQueryable <T> dataSource)
        {
            AjaxCallBackMode ajaxCallBackMode = this.AjaxCallBackMode;

            if (ajaxCallBackMode != AjaxCallBackMode.RequestData)
            {
                return(null);
            }

            HttpRequest request = HttpContext.Current.Request;

            if (request == null)
            {
                throw new Exception("Cannot Get Request Variable");
            }
            NameValueCollection queryString = request.HttpMethod == "POST" ? request.Form : request.QueryString;

            IQueryable <T> iqueryable = dataSource;

            Guard.IsNotNull(iqueryable, "DataSource", "should implement the IQueryable interface.");
            int    pageIndex     = this.GetPageIndex(queryString["page"]);
            int    num           = this.GetNum(queryString["rows"]);
            string text          = queryString["sidx"];
            string sortDirection = queryString["sord"];
            string parentRowID   = queryString["parentRowID"];
            bool   search        = (!string.IsNullOrEmpty(queryString["_search"]) && queryString["_search"] != "false");
            string filters       = queryString["filters"];
            string searchField   = queryString["searchField"];
            string searchString  = queryString["searchString"];
            string searchOper    = queryString["searchOper"];

            this.PagerSettings.CurrentPage = pageIndex;
            this.PagerSettings.PageSize    = num;
            if (search)
            {
                try
                {
                    if (string.IsNullOrEmpty(filters) && !string.IsNullOrEmpty(searchField))
                    {
                        iqueryable = iqueryable.Where(Util.GetWhereClause(this, searchField, searchString, searchOper));
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(filters))
                        {
                            iqueryable = iqueryable.Where(Util.GetWhereClause(this, filters));
                        }
                        else
                        {
                            if (this.ToolBarSettings.ShowSearchToolBar || search)
                            {
                                iqueryable = iqueryable.Where(Util.GetWhereClause(this, queryString));
                            }
                        }
                    }
                }
                catch (DataTypeNotSetException ex)
                {
                    throw ex;
                }
                catch (Exception)
                {
                    JsonResult jsonResult = new JsonResult();
                    jsonResult.JsonRequestBehavior = JsonRequestBehavior.DenyGet;
                    jsonResult.Data = new object();
                    return(jsonResult);
                }
            }
            int num2            = iqueryable.Count();
            int totalPagesCount = (int)Math.Ceiling((double)((float)num2 / (float)num));

            if (string.IsNullOrEmpty(text) && this.SortSettings.AutoSortByPrimaryKey)
            {
                if (this.Columns.Count == 0)
                {
                    throw new Exception("JQGrid must have at least one column defined.");
                }
                text          = Util.GetPrimaryKeyField(this);
                sortDirection = "asc";
            }
            if (!string.IsNullOrEmpty(text))
            {
                iqueryable = iqueryable.OrderBy(this.GetSortExpression(text, sortDirection));
            }
            iqueryable = iqueryable.Skip((pageIndex - 1) * num).Take(num);
            this.OnDataResolved(new JQGridDataResolvedEventArgs(this, iqueryable, this.DataSource as IQueryable));
            if (this.TreeGridSettings.Enabled)
            {
                JsonTreeResponse response = new JsonTreeResponse(pageIndex, totalPagesCount, num2, num, iqueryable.Count(), Util.GetFooterInfo(this));
                return(Util.ConvertToTreeJson(response, this, iqueryable));
            }
            JsonResponse response2 = new JsonResponse(pageIndex, totalPagesCount, num2, num, iqueryable.Count(), Util.GetFooterInfo(this));

            return(Util.ConvertToJson(response2, this, iqueryable));
        }