/
HomeController.cs
46 lines (37 loc) · 1.28 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace InfiniteScroll.Controllers
{
public class HomeController : Controller
{
AdventureWorksEntities _data = new AdventureWorksEntities();
const int recordsPerPage = 8;
public ActionResult Index(int? id)
{
var page = id ?? 0;
if (Request.IsAjaxRequest())
{
return PartialView("_Products", GetPaginatedProducts(page));
}
return View("Index", _data.Products.Where(x => x.ProductLine != null).Take(recordsPerPage));
}
public ActionResult Product(int? id)
{
var page = id ?? 0;
if (Request.IsAjaxRequest())
{
return PartialView("_Products", GetPaginatedProducts(page));
}
return View("Index", _data.Products.Where(x => x.ProductLine != null).Take(recordsPerPage));
}
private List<Product> GetPaginatedProducts(int page = 1)
{
var skipRecords = page * recordsPerPage;
return _data.Products.Where(x => x.ProductLine != null)
.OrderBy(x=>x.Name)
.Skip(skipRecords)
.Take(recordsPerPage).ToList();
}
}
}