예제 #1
0
        // GET: Cars
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page)
        {
            ViewBag.CurrentSort  = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            var cars = carRepository.FilterCars(HttpContext.User.Identity.Name);

            if (!String.IsNullOrEmpty(searchString))
            {
                cars = cars.Where(c => c.Brand.Contains(searchString) ||
                                  c.Model.Contains(searchString));
            }
            switch (sortOrder)
            {
            default:
                cars = cars.OrderBy(s => s.Model);
                break;
            }
            int pageSize   = 3;
            int pageNumber = (page ?? 1);

            return(View(cars.ToPagedList(pageNumber, pageSize)));
        }