Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PagingInfo
- {
- public int TotalItems { get; set; }
- public int ItemsPerPage { get; set; }
- public int CurrentPage { get; set; }
- public int TotalPages
- {
- get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
- }
- }
- public class QuestionsListViewModel
- {
- public IEnumerable<Question> Questions { get; set; }
- public PagingInfo PagingInfo { get; set; }
- }
- @foreach (var p in Model.Questions)
- {
- // render Questions
- }
- @Html.ActionLink("Load next 25 items", "Questions", "Controller", new { page = Model.PagingInfo.CurrentPage + 1 }))
- public ViewResult Questions(int page = 1)
- {
- QuestionsListViewModelmodel = new QuestionsListViewModel
- {
- // we need to get all items till now to render again
- Questions = repository.Questions
- .Take(page * PageSize),
- PagingInfo = new PagingInfo
- {
- CurrentPage = page,
- ItemsPerPage = 25,
- TotalItems = repository.Questions.Count()
- }
- };
- return View(model);
- }
- PM> Install-Package PagedList.Mvc
- public class QuestionViewModel
- {
- public int QuestionId { get; set; }
- public string QuestionName { get; set; }
- }
- using PagedList;
- public ActionResult Index(int? page)
- {
- var questions = new[] {
- new QuestionViewModel { QuestionId = 1, QuestionName = "Question 1" },
- new QuestionViewModel { QuestionId = 1, QuestionName = "Question 2" },
- new QuestionViewModel { QuestionId = 1, QuestionName = "Question 3" },
- new QuestionViewModel { QuestionId = 1, QuestionName = "Question 4" }
- };
- int pageSize = 3;
- int pageNumber = (page ?? 1);
- return View(questions.ToPagedList(pageNumber, pageSize));
- }
- @model PagedList.IPagedList<ViewModel.QuestionViewModel>
- @using PagedList.Mvc;
- <link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
- <table>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.QuestionId)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.QuestionName)
- </td>
- </tr>
- }
- </table>
- <br />
- Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
- @Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement