Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting and displaying records with paging

I am trying to count records returned from a search query.

The problem is that I am using the Model Count with paging. It will not display the Model Count for records not on the first page of the returned search query.

The code I am using:

 @String.Format("Total of {0} results", Model.Count()) @ViewBag.CurrentFilter

Model count ()) used for the counting of files

@ViewBag.CurrentFilter is used for the name of the search query

Below is the code for the controler

            int pageSize = 4;
            int pageNumber = (page ?? 1);

            var carsviewd = cars.ToPagedList(pageNumber, pageSize);
            if (carsviewd.Any())
            {
                return View(carsviewd);
            }
            return RedirectToAction("NoResult");
        }


    }
like image 629
Daza Aza Avatar asked Jan 27 '26 15:01

Daza Aza


1 Answers

In controller before return view,you can use:

ViewBag.Count =  cars.Count();

then in the View you can use the following:

@String.Format("Total of {0} results", ViewBag.Count)
like image 101
artwl Avatar answered Jan 29 '26 13:01

artwl