Example #1
0
        public JsonResult GetData([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
        {
            // get all users
            var data         = _faqRepository.AllIncluding(x => x.User).ToList().Select(x => new FAQViewModel(x));
            var filteredData = data;
            var searchValue  = "";

            // filter data
            if (!string.IsNullOrWhiteSpace(requestModel.Search.Value))
            {
                searchValue  = requestModel.Search.Value.ToLower();
                filteredData = filteredData.Where(x =>
                                                  x.Title.ToLower().Contains(searchValue) ||
                                                  x.Editor.ToLower().Contains(searchValue));
            }

            // get sorting
            var sortColumns = string.Join(", ",
                                          requestModel.Columns.GetSortedColumns()
                                          .Select(
                                              x => string.Format("{0} {1}", x.Data, x.SortDirection.ToString().ToLower().Contains("asc") ? "ASC" : "DESC")
                                              ).ToArray());

            // get paged data
            var pagedData = filteredData
                            .OrderBy(sortColumns)
                            .Skip(requestModel.Start)
                            .Take(requestModel.Length);

            var result = new DataTablesResponse(requestModel.Draw, pagedData, data.Count(), data.Count());

            return(Json(result));
        }