jeudi 14 janvier 2016

MVC paging doesnt include list items if size is less than page size

I have the following scenario where if user selects a value in the filter for example id which is a property of the IEnumerable domain object in the view model and if the current page is for example 2 and the resulting query count is less than the static page size 10, there is some sort of error in the paging and viewmodel IEnumerable list will be empty even before the paging and the query has results.

It only works for result sets which are greater than 10 and it doesn't matter on what page the filters were applied. It seems that following line is causing smaller results to be excluded. If the filter is applied on the first page with results less than 10 it works.

ErrorCode = errocodeViewModel.ToList().Skip((page - 1) * PageSize).Take(PageSize),

 [HttpPost]
        public ActionResult List(int PageNo, string ActiveErrors, ErrorCodeFilter errorCodeFilter = null)
        {
            if (PageNo == 0)
                PageNo = 1;
            int page = PageNo;


            if (ActiveErrors == "N")
                page = 1;

            var errocodeViewModel = GetErrorCodeListQuery(errorCodeFilter, ActiveErrors).ToList();
            var totalErrorCodeCount = errocodeViewModel.Count();

            ErrorCodesListViewModel viewModel = new ErrorCodesListViewModel()
            {
                ErrorCode = errocodeViewModel.ToList().Skip((page - 1) * PageSize).Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = Convert.ToInt32(page),
                    ItemsPerPage = PageSize,
                    TotalItems = totalErrorCodeCount
                }
            };

            return View(viewModel);
        }


public class ErrorCodesListViewModel
    {
        public IEnumerable<ErrorCodeViewModel> ErrorCode { get; set; }
        public PagingInfo PagingInfo { get; set; }
        public ErrorCodeFilter ErrorCodeFilter;
        public int PageNo;
}

Aucun commentaire:

Enregistrer un commentaire