コード例 #1
0
 // GET: Cars
 public ActionResult Index()
 {
     try
     {
         IQueryable <Car> cars = db.Cars;
         if (cars != null)
         {
             CarsIndexViewModel model = new CarsIndexViewModel()
             {
                 Makes = cars.Select(c => new SelectListItem()
                 {
                     Value = c.make, Text = c.make
                 }).Distinct().ToList(),
                 Models    = new List <SelectListItem>(),
                 GridModel = new CarGridViewModel()
                 {
                     Cars = PagedListHelpers.GetNextCars(null, null, null, 0, 10)
                 }
             };
             return(View(model));
         }
         else
         {
             throw new Exception("The 'Car' entity result collection is null.", new NoNullAllowedException());
         }
     }
     catch (Exception ex)
     {
         return(View("Error", new HandleErrorInfo(ex, "Cars", "Index")));
     }
 }
コード例 #2
0
        // GET: Cars
        public async Task <IActionResult> Index(CarsIndexViewModel model)
        {
            model.Pager ??= new PagerViewModel();
            model.Pager.CurrentPage = model.Pager.CurrentPage <= 0 ? 1 : model.Pager.CurrentPage;

            List <CarsViewModel> items = await _context.Cars.Skip((model.Pager.CurrentPage - 1) *PageSize).Take(PageSize).Select(c => new CarsViewModel()
            {
                Id     = c.Id,
                Brand  = c.Brand,
                Engine = c.Engine,
                Model  = c.Model,
                Year   = c.Year
            }).ToListAsync();

            model.Items            = items;
            model.Pager.PagesCount = (int)Math.Ceiling(await _context.Cars.CountAsync() / (double)PageSize);

            return(View(model));
        }