M
M
Michael2015-03-10 20:12:08
ASP.NET
Michael, 2015-03-10 20:12:08

Pagination (pagination) ASP.NET MVC?

Good day to all, I ran into the task of paging output, I set everything up, for this case, but I ran into a problem. I have query results filtered on the page, according to the criteria, there can be 100500 results, and I would like them to be displayed page by page too. But this does not happen when you go to the next page of filtered results, all data is selected again (not sorted, so to speak).
I'll attach the controller code.

public ActionResult Index(int page = 1)
        {
            ViewListAuto model = new ViewListAuto
            {
                ListAuto = rep.GetAllShortAdvert().OrderByDescending(p => p.IdComment).Skip((page - 1) * countItemPage).Take(countItemPage),
                Pager = new Tools.Pager
                {
                    CountItemInTable = rep.GetAllShortAdvert().Count(),
                    CountItemPage = countItemPage,
                    CurrentPage = page
                }
            };
            return View(model);
        }
[HttpPost]
 public ActionResult Index(ViewListAuto model, int page = 1)
        {
            FilterAutoValue filter = new FilterAutoValue
            {
                IdBrand = model.BrandName,
                IdModel = model.ModelName,
                TypeTransmission = model.TypeTransmission,
                YearManufacture = Convert.ToInt32(model.YearManufacture)
            };
            string query = dataFilter.FiltrationAuto(filter);
             filterAutoList = db.ShortAdvert.SqlQuery(query).OrderByDescending(p => p.IdComment).Skip((page - 1) * countItemPage).Take(countItemPage).ToList();

            model.ListAuto = filterAutoList.OrderByDescending(p => p.IdComment).Skip((page - 1) * countItemPage).Take(countItemPage);
            model.Pager = new Tools.Pager
            {
                CountItemInTable = filterAutoList.Count(),
                CountItemPage = countItemPage,
                CurrentPage = page
            };
            return View(model);
        }

I've been doing MVC for a month now, everything seems to be working out, but here .. this is the problem.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Rodyushkin, 2015-04-02
@MickMS

It seems that the problem is that the method that takes filter arguments only works on POST, and the pager is implemented as links, which obviously make a GET request (and you get into the first method).
I think you could make one GET method, Index(FilterAutoValue filter, int page = 1)and if no search criteria is set, just return from dataFilter.FiltrationAutothe request displaying everything.

C
CrazyHorse, 2015-03-10
@CrazyHorse

Look towards the correct generation of links, or routing - the page parameter when you click on the link comes to Action? everything should become clear here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question