Практика по ресурсу Metanit
Модель
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace modelsTraining2.Models { public class Phone { public int Id { get; set; } public string Model { get; set; } public string Producer { get; set; } } } |
Контроллер
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 |
using modelsTraining2.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace modelsTraining2.Controllers { public class PaginatorController:Controller { List<Phone> phones; public PaginatorController() { phones = new List<Phone>(); phones.Add(new Phone { Id = 1, Model = "Samsung Galaxy III", Producer = "Samsung" }); phones.Add(new Phone { Id = 2, Model = "Samsung Ace II", Producer = "Samsung" }); phones.Add(new Phone { Id = 3, Model = "HTC Hero", Producer = "HTC" }); phones.Add(new Phone { Id = 4, Model = "HTC One S", Producer = "HTC" }); phones.Add(new Phone { Id = 5, Model = "HTC One X", Producer = "HTC" }); phones.Add(new Phone { Id = 6, Model = "LG Optimus 3D", Producer = "LG" }); phones.Add(new Phone { Id = 7, Model = "Nokia N9", Producer = "Nokia" }); phones.Add(new Phone { Id = 8, Model = "Samsung Galaxy Nexus", Producer = "Samsung" }); phones.Add(new Phone { Id = 9, Model = "Sony Xperia X10", Producer = "SONY" }); phones.Add(new Phone { Id = 10, Model = "Samsung Galaxy II", Producer = "Samsung" }); } public ActionResult Index(int page = 1) { int pageSize = 3; // количество объектов на страницу IEnumerable<Phone> phonesPerPages = phones.Skip((page - 1) * pageSize).Take(pageSize); PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = phones.Count }; IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Phones = phonesPerPages }; return View(ivm); } } } |
Хэлпер
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace modelsTraining2.Models { using modelsTraining2.Models; using System.Text; using System.Web; using System.Web.Mvc; //............................. public static class PagingHelpers { public static MvcHtmlString PageLinks(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pageInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); // если текущая страница, то выделяем ее, // например, добавляя класс if (i == pageInfo.PageNumber) { tag.AddCssClass("selected"); tag.AddCssClass("btn-primary"); } tag.AddCssClass("btn btn-default"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); } } } |
Представление
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@model modelsTraining2.Models.IndexViewModel @using modelsTraining2.Models @{ ViewBag.Title = "Home Page"; } <table class="table"> <tr> <td>Модель</td> <td>Производитель</td> </tr> @foreach (var item in Model.Phones) { <tr> <td>@item.Model</td> <td>@item.Producer</td> </tr> } </table> <br /> <div class="btn-group"> @Html.PageLinks(Model.PageInfo, x => Url.Action("Index", new { page = x })) </div> |
Результат