HtmlHelper 不包含 PagedListPager 的定义
HtmlHelper does not contain a definition for PagedListPager
我是 ASP.NET MVC 的新手,所以这个问题可能不会太复杂。我在向 ASP.NET MVC 项目添加分页时遇到问题。我已经从 NuGet 包安装程序安装了 pagedlist.mvc,然后在控制器中编写了这个简单的索引代码来传递分页列表:
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MvcMovie.Data;
using MvcMovie.Models;
using PagedList;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
private readonly MvcMovieContext _context;
public MoviesController(MvcMovieContext context)
{
_context = context;
}
// GET: Movies
public IActionResult Index(int ? page)
{
var movies = from m in _context.Movies select m;
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(movies.ToPagedList(pageNumber,pageSize));
}
然后,我更改了索引视图并在顶部添加了 @using PagedList.Mvc
。我还将模型更改为 @model PagedList.IPagedList<MvcMovie.Models.Movie>
。视图文件如下所示:
@using PagedList.Mvc;
@model PagedList.IPagedList<MvcMovie.Models.Movie>
@{
ViewBag.Title = "Movie List";
}
<h1>Listă de filme</h1>
<p>
<a asp-action="Create">Adaugă film</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Title)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.MovieId">Editează</a> |
<a asp-action="Details" asp-route-id="@item.MovieId">Detalii</a> |
<a asp-action="Delete" asp-route-id="@item.MovieId">Șterge film</a>
</td>
</tr>
}
</tbody>
</table>
<br />
<div style="margin:5px;">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
如您所见,我在视图底部编写了显示分页的代码,但出现错误:
'IHtmlHelper<IPagedList>' does not contain a definition for
'PagedListPager' and the best extension method overload
'HtmlHelper.PagedListPager(HtmlHelper, IPagedList, Func<int, string>)'
requires a receiver of type 'HtmlHelper' MvcMovie
我也试过在视图文件的顶部添加 @using PagedList
,但它什么也没做。
您缺少 Paggedlist.You 的程序集引用之一需要从 nuget 包安装两个包
分页列表和
PagedList.mvc(参考下图)
Click here for image references
如果你已经安装然后重新安装它们或重新启动项目
我是 ASP.NET MVC 的新手,所以这个问题可能不会太复杂。我在向 ASP.NET MVC 项目添加分页时遇到问题。我已经从 NuGet 包安装程序安装了 pagedlist.mvc,然后在控制器中编写了这个简单的索引代码来传递分页列表:
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MvcMovie.Data;
using MvcMovie.Models;
using PagedList;
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
private readonly MvcMovieContext _context;
public MoviesController(MvcMovieContext context)
{
_context = context;
}
// GET: Movies
public IActionResult Index(int ? page)
{
var movies = from m in _context.Movies select m;
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(movies.ToPagedList(pageNumber,pageSize));
}
然后,我更改了索引视图并在顶部添加了 @using PagedList.Mvc
。我还将模型更改为 @model PagedList.IPagedList<MvcMovie.Models.Movie>
。视图文件如下所示:
@using PagedList.Mvc;
@model PagedList.IPagedList<MvcMovie.Models.Movie>
@{
ViewBag.Title = "Movie List";
}
<h1>Listă de filme</h1>
<p>
<a asp-action="Create">Adaugă film</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Title)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.MovieId">Editează</a> |
<a asp-action="Details" asp-route-id="@item.MovieId">Detalii</a> |
<a asp-action="Delete" asp-route-id="@item.MovieId">Șterge film</a>
</td>
</tr>
}
</tbody>
</table>
<br />
<div style="margin:5px;">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
如您所见,我在视图底部编写了显示分页的代码,但出现错误:
'IHtmlHelper<IPagedList>' does not contain a definition for 'PagedListPager' and the best extension method overload 'HtmlHelper.PagedListPager(HtmlHelper, IPagedList, Func<int, string>)' requires a receiver of type 'HtmlHelper' MvcMovie
我也试过在视图文件的顶部添加 @using PagedList
,但它什么也没做。
您缺少 Paggedlist.You 的程序集引用之一需要从 nuget 包安装两个包
分页列表和 PagedList.mvc(参考下图)
Click here for image references
如果你已经安装然后重新安装它们或重新启动项目