【发布时间】:2021-07-12 05:12:34
【问题描述】:
我是 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
' 不包含对 'PagedListPager' 和最好的扩展方法重载 'HtmlHelper.PagedListPager(HtmlHelper, IPagedList, Func )' 需要 'HtmlHelper' MvcMovie 类型的接收器
我也尝试在视图文件的顶部添加@using PagedList,但它什么也没做。
【问题讨论】:
标签: c# html asp.net-mvc pagination pagedlist