Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. public class PagingInfo
  2. {
  3. public int TotalItems { get; set; }
  4. public int ItemsPerPage { get; set; }
  5. public int CurrentPage { get; set; }
  6. public int TotalPages
  7. {
  8. get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
  9. }
  10. }
  11.  
  12. public class QuestionsListViewModel
  13. {
  14. public IEnumerable<Question> Questions { get; set; }
  15. public PagingInfo PagingInfo { get; set; }
  16. }
  17.  
  18. @foreach (var p in Model.Questions)
  19. {
  20. // render Questions
  21. }
  22.  
  23. @Html.ActionLink("Load next 25 items", "Questions", "Controller", new { page = Model.PagingInfo.CurrentPage + 1 }))
  24.  
  25. public ViewResult Questions(int page = 1)
  26. {
  27. QuestionsListViewModelmodel = new QuestionsListViewModel
  28. {
  29. // we need to get all items till now to render again
  30. Questions = repository.Questions
  31. .Take(page * PageSize),
  32. PagingInfo = new PagingInfo
  33. {
  34. CurrentPage = page,
  35. ItemsPerPage = 25,
  36. TotalItems = repository.Questions.Count()
  37. }
  38. };
  39.  
  40. return View(model);
  41. }
  42.  
  43. PM> Install-Package PagedList.Mvc
  44.  
  45. public class QuestionViewModel
  46. {
  47. public int QuestionId { get; set; }
  48. public string QuestionName { get; set; }
  49. }
  50.  
  51. using PagedList;
  52.  
  53. public ActionResult Index(int? page)
  54. {
  55. var questions = new[] {
  56. new QuestionViewModel { QuestionId = 1, QuestionName = "Question 1" },
  57. new QuestionViewModel { QuestionId = 1, QuestionName = "Question 2" },
  58. new QuestionViewModel { QuestionId = 1, QuestionName = "Question 3" },
  59. new QuestionViewModel { QuestionId = 1, QuestionName = "Question 4" }
  60. };
  61.  
  62. int pageSize = 3;
  63. int pageNumber = (page ?? 1);
  64. return View(questions.ToPagedList(pageNumber, pageSize));
  65. }
  66.  
  67. @model PagedList.IPagedList<ViewModel.QuestionViewModel>
  68. @using PagedList.Mvc;
  69. <link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
  70.  
  71.  
  72. <table>
  73.  
  74. @foreach (var item in Model) {
  75. <tr>
  76. <td>
  77. @Html.DisplayFor(modelItem => item.QuestionId)
  78. </td>
  79. <td>
  80. @Html.DisplayFor(modelItem => item.QuestionName)
  81. </td>
  82. </tr>
  83. }
  84.  
  85. </table>
  86.  
  87. <br />
  88.  
  89. Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
  90. @Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement