-
Notifications
You must be signed in to change notification settings - Fork 16
/
HomeController.cs
64 lines (56 loc) · 1.98 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using MvcInfiniteScrollGridDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcInfiniteScrollGridDemo.Controllers
{
public class HomeController : Controller
{
public const int RecordsPerPage = 20;
public HomeController()
{
ViewBag.RecordsPerPage = RecordsPerPage;
}
public ActionResult Index()
{
return RedirectToAction("GetCustomers");
}
public ActionResult GetCustomers(int? pageNum)
{
pageNum = pageNum ?? 0;
ViewBag.IsEndOfRecords = false;
if (Request.IsAjaxRequest())
{
var customers = GetRecordsForPage(pageNum.Value);
ViewBag.IsEndOfRecords = (customers.Any()) && ((pageNum.Value * RecordsPerPage) >= customers.Last().Key);
return PartialView("_CustomerRow", customers);
}
else
{
LoadAllCustomersToSession();
ViewBag.Customers = GetRecordsForPage(pageNum.Value);
return View("Index");
}
}
public void LoadAllCustomersToSession()
{
var customerRepo = new CustomerRepository();
var customers = customerRepo.ListCustomers();
int custIndex = 1;
Session["Customers"] = customers.ToDictionary(x => custIndex++, x => x);
ViewBag.TotalNumberCustomers = customers.Count();
}
public Dictionary<int, Customer> GetRecordsForPage(int pageNum)
{
Dictionary<int, Customer> customers = (Session["Customers"] as Dictionary<int, Customer>);
int from = (pageNum * RecordsPerPage);
int to = from + RecordsPerPage;
return customers
.Where(x => x.Key > from && x.Key <= to)
.OrderBy(x => x.Key)
.ToDictionary(x => x.Key, x => x.Value);
}
}
}