我做了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),我真的迷失了!
請你能說一點更具體什麼不工作。 – ngm 2012-07-17 21:23:04
如果您可以發佈IPagedList示例的來源,它也會有所幫助。 – ngm 2012-07-17 21:42:28
我調試並運行,我可以看到數據庫中所有保存的記錄列表,但pagedlist不工作,不會出現在頁面的底部,我從這裏獲得代碼http:// forums。 ASP。net/t/1678594.aspx/1/10與一些撰稿人所做的更正 – Ivelisse 2012-07-18 02:39:19