0

我在一个网站上工作,但链接有问题。

我的网站: 我的网站 (我有第二页和第三页,它们是一样的)

书本网站:书本网站

问题:不要放置通向其他页面的链接。

我的代码:

控制器:

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>
4

1 回答 1

0

看看UrlHelper.GenerateUrl ASP.NET MVC 中的 UrlHelper.GenerateUrl 可用于返回包含 URL 的字符串。

于 2013-10-14T09:33:30.987 回答