2013-10-13 66 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字符串。