ASP.Net MVC 3页面链接

时间:2013-10-13 15:26:19

标签: asp.net asp.net-mvc asp.net-mvc-3

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

我的网站: 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>

1 个答案:

答案 0 :(得分:0)

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