public int SetupGridParams(GridModel model)
        {
            if (string.IsNullOrWhiteSpace(model.SearchFor))
            {
                model.SearchFor = "null";
            }
            if (string.IsNullOrWhiteSpace(model.SortKey))
            {
                model.SortKey = string.Empty;
            }
            if (string.IsNullOrWhiteSpace(model.SortOrder))
            {
                model.SortOrder = "ASC";
            }

            model.SortKey = model.SortKey.ToLower();

            if (model.CurrentPage == null)
            {
                model.CurrentPage = 1;
            }
            if (model.RecordsPerPage == null)
            {
                model.RecordsPerPage = 100;
            }

            int begin = (model.CurrentPage.Value - 1) * model.RecordsPerPage.Value;
            return begin;
        }
Ejemplo n.º 2
0
        public ActionResult<JobsGridModel> JobsGrid(GridModel model)
        {
            TestControllerBase testControllerBase = new TestControllerBase();
            JobsGridModel jobsGridModel = new JobsGridModel();
            int begin = testControllerBase.SetupGridParams(model);
            var filteredQuery = provider.PostJobs().Select(a => new JobsGridModel()
            {
                Client = jobsGridModel.Client,
                DueDate = jobsGridModel.DueDate,
                JobName = jobsGridModel.JobName,
                JobNumber = jobsGridModel.JobNumber,
                Status = jobsGridModel.Status
            });

            if (model.SearchFor != "null")
            {
                filteredQuery = filteredQuery.Where(
                                              a => a.Client.Contains(model.SearchFor)
                                              || a.JobName.Contains(model.SearchFor));
            }

            var totalNumberOfRecords = filteredQuery.Count();

            if (String.IsNullOrWhiteSpace(model.SortKey))
            {
                filteredQuery = filteredQuery.OrderBy(a => a.Client);
            }

            if (!string.IsNullOrWhiteSpace(model.SortKey))
            {
                model.SortKey = model.SortKey.ToLower();

            }
            switch (model.SortOrder)
            {
                case "ASC":
                    switch (model.SortKey)
                    {
                        case "client":
                            filteredQuery = filteredQuery.OrderBy(a => a.Client);
                            break;
                        case "duedate":
                            filteredQuery = filteredQuery.OrderBy(a => a.DueDate);
                            break;
                        case "jobname":
                            filteredQuery = filteredQuery.OrderBy(a => a.JobName);
                            break;
                        case "jobnumber":
                            filteredQuery = filteredQuery.OrderBy(a => a.JobNumber);
                            break;
                        case "status":
                            filteredQuery = filteredQuery.OrderBy(a => a.Status);
                            break;
                    }
                    break;
                case "DESC":
                    switch (model.SortKey)
                    {
                        case "client":
                            filteredQuery = filteredQuery.OrderBy(a => a.Client);
                            break;
                        case "duedate":
                            filteredQuery = filteredQuery.OrderBy(a => a.DueDate);
                            break;
                        case "jobname":
                            filteredQuery = filteredQuery.OrderBy(a => a.JobName);
                            break;
                        case "jobnumber":
                            filteredQuery = filteredQuery.OrderBy(a => a.JobNumber);
                            break;
                        case "status":
                            filteredQuery = filteredQuery.OrderBy(a => a.Status);
                            break;
                    }
                    break;
            }
            filteredQuery = filteredQuery.Skip(begin).Take(model.RecordsPerPage.Value);

            return new ActionResult<JobsGridModel>(filteredQuery.ToList(), totalNumberOfRecords);
        }