2013-10-13 65 views
0

我正在一個網站上工作,並有鏈接問題。ASP.Net MVC 3頁鏈接

我的網站:http: My Web-Site (我已經第二次和第三頁,它們是同一個),從書

網站:http: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> 
+0

你能重新框架你的問題嗎? – HaBo

+0

是不是有沒有使用@Html.ActionLink(「Action」,「Controller」)的原因? –

+0

@HaBo我需要鏈接到每個頁面,但它只顯示一個鏈接,如照片(我的網站) –

回答

0

UrlHelper.GenerateUrl 看一看的UrlHelper.GenerateUrl在ASP.NET MVC可以用來返回其中包含URL字符串。