2012-07-17 45 views
0

我做了Rick Anderson的ASP.NET MVC 3(C#)入门教程,是一个产品目录,已经在工作,但是,由于我添加了一长串产品,现在我需要一个pagedList得到的只是一些每页产品,环顾四周,我发现一个例子,但不工作,我的项目,我添加的模型文件,这个类名为IPagedList.cs分页列表不起作用

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Presupuestos.Models 
{ 
    public interface IPagedList 
    { 
     int ItemCount 
     { 
      get; 
      set; 
     } 

     int PageCount 
     { 
      get; 
      set; 
     } 

     int PageIndex 
     { 
      get; 
      set; 
     } 

     int PageSize 
     { 
      get; 
      set; 
     } 

     bool IsPreviousPage 
     { 
      get; 
     } 

     bool IsNextPage 
     { 
      get; 
     } 
    } 

    public interface IPagedList<T> : IList<T>, IPagedList 
    { 
    } 

    public class PagedList<T> : List<T>, IPagedList<T> 
    { 
     private List<Productos> list; 
     private int p; 
     private int p_2; 

     public PagedList(IQueryable<T> source, int index, int pageSize) 
     { 
      this.ItemCount = source.Count(); 
      this.PageSize = pageSize; 
      this.PageIndex = index; 
      this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); 
      this.PageCount = (int)Math.Ceiling((double)this.ItemCount/this.PageSize); 
     } 

     public PagedList(List<T> source, int index, int pageSize) 
     { 
      this.ItemCount = source.Count(); 
      this.PageSize = pageSize; 
      this.PageIndex = index; 
      this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); 
     } 

     public PagedList(List<Productos> list, int p, int p_2) 
     { 
      // TODO: Complete member initialization 
      this.list = list; 
      this.p = p; 
      this.p_2 = p_2; 
     } 

     public int ItemCount 
     { 
      get; 
      set; 
     } 

     public int PageCount 
     { 
      get; 
      set; 
     } 

     public int PageIndex 
     { 
      get; 
      set; 
     } 

     public int PageSize 
     { 
      get; 
      set; 
     } 

     public bool IsPreviousPage 
     { 
      get 
      { 
       return (PageIndex > 0); 
      } 
     } 

     public bool IsNextPage 
     { 
      get 
      { 
       return (PageIndex + 1) * PageSize <= ItemCount; 
      } 
     } 
    } 

    public static class Pagination 
    { 
     public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize) 
     { 
      return new PagedList<T>(source, index, pageSize); 
     } 

     public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index) 
     { 
      return new PagedList<T>(source, index, 10); 
     } 
    } 
} 

而且,我添加了另一个类名为HTMLHelpers.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Text; 
using Presupuestos.Models; 

namespace Presupuestos.Models 
{ 
    public static class ListPaging 
    { 
     public static MvcHtmlString Paging(this HtmlHelper html, IPagedList pagedList, string url, string pagePlaceHolder) 
     { 
      StringBuilder sb = new StringBuilder(); 
      // only show paging if we have more items than the page size 
      if (pagedList.ItemCount > pagedList.PageSize) 
      { 
       sb.Append("<ul class=\"paging\">"); 
       if (pagedList.IsPreviousPage && pagedList.PageIndex != 1) 
       { 
        // previous link 
        sb.Append("<li class=\"prev\"><a href=\""); 
        sb.Append(url.Replace(pagePlaceHolder, (pagedList.PageIndex - 1).ToString())); 
        sb.Append("\" title=\"Go to Previous Page\">prev</a></li>"); 
       } 
       for (int i = 0; i < pagedList.PageCount; i++) 
       { 
        sb.Append("<li>"); 
        if (i == pagedList.PageIndex) 
        { 
         sb.Append("<span>").Append((i + 1).ToString()).Append("</span>"); 
        } 
        else 
        { 
         sb.Append("<a href=\""); 
         sb.Append(url.Replace(pagePlaceHolder, (i + 1).ToString())); 
         sb.Append("\" title=\"Go to Page ").Append((i + 1).ToString()); 
         sb.Append("\">").Append((i + 1).ToString()).Append("</a>"); 
        } 
        sb.Append("</li>"); 
       } 
       if (pagedList.IsNextPage) 
       { 
        // next link 
        sb.Append("<li class=\"next\"><a href=\""); 
        sb.Append(url.Replace(pagePlaceHolder, (pagedList.PageIndex + 1).ToString())); 
        sb.Append("\" title=\"Go to Next Page\">next</a></li>"); 
       } 
       sb.Append("</ul>"); 
      } 
      return MvcHtmlString.Create(sb.ToString()); 
     } 
    } 
} 

最后,这是视图文件,我刚添加@u在最后唱presupuestos.models最后html.paging:

@model IEnumerable<Presupuestos.Models.Productos>   
@using Presupuestos.Models 

@{ 
    ViewBag.Title = "Productos"; 
} 

<h2>Catalogo de Productos</h2> 

<p> 
    @Html.ActionLink("Agregar Producto", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      Marca 
     </th> 
     <th> 
      Codigo 
     </th> 
     <th> 
      Nombre 
     </th> 
     <th> 
      Envase 
     </th> 
     <th> 
      Presentación 
     </th> 
     <th> 
      Linea 
     </th> 
     <th> 
      Categoria 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) 
{ 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.marca) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.codigo) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.nombre) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.envase) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.presentación) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.linea) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.categoria) 
     </td> 
     <td> 
      @Html.ActionLink("Editar", "Edit", new { id = item.ID }) | 
      @Html.ActionLink("Detalles", "Details", new { id = item.ID }) | 
      @Html.ActionLink("Borrar", "Delete", new { id = item.ID }) 
     </td> 
    </tr> 
} 

</table> 
<div> 
@Html.Paging(new PagedList<Productos>(ViewData.Model.ToList(),1,10), Url.Action("Index","Index", new { page = "PAGENUM" }), "PAGENUM") 
</div> 

希望你能帮助我,我一直在坚持这一天,就在上周五,我开始使用MVC3,好的事情是什么我的老板需要的是教程中的内容,但是,现在我想要做这些额外的事情(pagedlist),我真的迷失了!

+0

请你能说一点更具体什么不工作。 – ngm 2012-07-17 21:23:04

+0

如果您可以发布IPagedList示例的来源,它也会有所帮助。 – ngm 2012-07-17 21:42:28

+0

我调试并运行,我可以看到数据库中所有保存的记录列表,但pagedlist不工作​​,不会出现在页面的底部,我从这里获得代码http:// forums。 ASP。net/t/1678594.aspx/1/10与一些撰稿人所做的更正 – Ivelisse 2012-07-18 02:39:19

回答

0

这看起来好像在这条线

@Html.Paging(new PagedList<Productos>(ViewData.Model.ToList(),1,10), Url.Action("Index","Index", new { page = "PAGENUM" }), "PAGENUM") 

你硬编码的当前页面的索引为1

我猜你Index操作方法,你需要可能想拥有的东西代表您可以传递到您的视图的当前页面,并将硬编码1替换为您的视图。

例如

public void Index(int page = 1) 
{ 
    // ... set up your view model 
    // ... 
    // ... 

    // page is added to the url by your paging helper. 
    ViewBag.CurrentPage = page; 

    return View(viewModel); 
} 

并在视图...

@Html.Paging(new PagedList<Productos>(ViewData.Model.ToList(),@ViewBag.CurrentPage,10), Url.Action("Index","Index", new { page = "PAGENUM" }), "PAGENUM") 
+0

我编辑的变化看起来像这样的'公共ViewResult索引(int page = 1) { ViewBag.CurrentPage = page; return View db.Productos.ToList()); }'并粘贴你在视图中写的东西,但仍然没有任何反应 – Ivelisse 2012-07-18 02:43:47

0

根据你在您的评论说,该分页列表不显示在页面的底部,我真的建议做一些调试并查看哪些代码路径实际上正在执行。但无论如何,我们可以猜到以下...

第一步是在ListPaging.Paging中放置一个断点并检查它实际上是否被调用。如果是,并且没有任何东西被追加到您的StringBuilder中,那么可能

if (pagedList.ItemCount > pagedList.PageSize) 
{ 
    // ... 

不计算为真。我假设列表中的项目数量超过10个,所以它应该是真的。所以也许这不是评估为真的原因是因为没有设置价值。

您添加的构造看起来半信半疑:

public PagedList(List<Productos> list, int p, int p_2) 
{ 
    // TODO: Complete member initialization 
    this.list = list; 
    this.p = p; 
    this.p_2 = p_2; 
} 

是PagedList采用甚至可以在这里设置的属性无。这个构造函数有什么意义?什么是pp_2?如果这个构造函数被调用,那么ItemCount和PageSize将没有任何价值。我建议摆脱它,让其他构造函数被调用。

+0

是的!我摆脱它,我正在寻找一种新的方式来做到这一点,谢谢! – Ivelisse 2012-07-18 16:54:22