我在添加了错误后显示的所有代码后,在我的项目中添加了分页。
编译器错误消息:CS1061: 'System.Collections.Generic.IEnumerable'可以 不包含'分页'的定义,也没有扩展方法 '分页'接受第一个类型的论证 'System.Collections.Generic.IEnumerable'可以 找到(你错过了使用指令或汇编引用吗?)
查看:
<h2
Online Store
</h2>
@foreach (var item in Model)
{
<div style="display:inline-block">
<div style="width:30%; float:left;">
<img src="@item.Image" width="170" height="276" alt="@item.ProductName" title="@item.ProductName">
</div>
<div style="width:70%; float:left;">
<h3>
@Html.DisplayFor(modelItem => item.ProductName)
</h3>
<p>
@Html.DisplayFor(modelItem => item.Description)
</p>
<p>
$@Html.DisplayFor(modelItem => item.Price)
</p>
<p>
@Html.ActionLink("More Info", "Index", "ItemDetail", new { id = item.ProductID }, new { id = "ProductID" })
</p>
</div>
</div> }
<div class="pager">
@Html.PageLinks(Model.Pagination, x => Url.Action("Index", new { page = x }))
型号:
namespace ISkills.Models
{
public class PaginationModel
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages
{
get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
}
控制器:
private ISkillsContext db = new ISkillsContext();
private const int PAGE_SIZE = 3;
public ActionResult Index( int page =1)
{
var data = db.Products.Select(p => p)
.OrderBy(p => p.ProductName)
.Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE);
ProductsModel model = new ProductsModel
{
Products = data,
Pagination = new PaginationModel
{
CurrentPage = page,
ItemsPerPage = PAGE_SIZE,
TotalItems = db.Products.ToList().Count()
}
};
HTML帮助:
namespace ISkills.HtmlHelpers
{
public static class PaginationHelper
{
public static MvcHtmlString PageLinks(this HtmlHelper htmlHelper,
PaginationModel pagination,
Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= pagination.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagination.CurrentPage)
{
tag.AddCssClass("selected");
tag.AddCssClass("btn-primary");
}
tag.AddCssClass("btn btn-default");
result.Append(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
}
任何人都可以帮助我,如何解决此错误?