我在网站上工作,但链接有问题。
我的网站: My Web-Site (我有第二页和第三页,它们是相同的)
网站来自书籍:Web-Site from book
问题:请勿放置指向其他页面的链接。
我的代码:
控制器:
public class ProductController : Controller
{
public int PageSize = 4;
private IProductRepository repository;
public ProductController(IProductRepository productRepository)
{
repository = productRepository;
}
public ViewResult List(int page = 1)
{
ProductsListViewModel viewModel = new ProductsListViewModel{
Products=repository.Products
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurentPage = page,
ItemsPerPage = PageSize,
TotalItem = repository.Products.Count()
}
};
return View(viewModel);
}
的HtmlHelper:
public static class PagingHelpers
{
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurentPage)
{
tag.AddCssClass("selected");
result.Append(tag.ToString());
}
}
return MvcHtmlString.Create(result.ToString());
}
}
型号:
public class PagingInfo
{
public int TotalItem { get; set; }
public int ItemsPerPage { get; set; }
public int CurentPage { get; set; }
public int TotalPages {
get { return (int)Math.Ceiling((decimal)TotalItem / ItemsPerPage); }
}
}
和
public class ProductsListViewModel
{
public IEnumerable<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
}
查看:
@model SportsStore.WebUI.Models.ProductsListViewModel
@foreach(var a in Model.Products)
{
<div >
<h4>@a.Name</h4>
<h4>@a.Description</h4>
<h4>@a.Price.ToString("c")</h4>
</div>
}
<div class="pager">
@Html.PageLinks(Model.PagingInfo,x=>Url.Action("List", new {page=x}))
</div>