gpt4 book ai didi

c# - PagedList MVC 不包含 PagedListPager 的定义

转载 作者:太空狗 更新时间:2023-10-29 22:30:22 25 4
gpt4 key购买 nike

我不知道为什么 PageCount、PageNumber 和 PageListPager 不包含定义或无法找到。它询问我是否缺少 using 指令或程序集引用,但我在我的用户 Controller 中有它。

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
to
@Html.PagedListPager( Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter }) )

注意:以下图片和代码仅供引用

Database Error

Visual Studio error

Controller \UserController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RecreationalServicesTicketingSystem.Models;
using RecreationalServicesTicketingSystem.DAL;
using PagedList;

namespace RecreationalServicesTicketingSystem.Controllers
{
public class UserController : Controller
{
private IssueContext db = new IssueContext();

//
// GET: /User/

public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}

ViewBag.CurrentFilter = searchString;

var users = from s in db.Users
select s;
if (!String.IsNullOrEmpty(searchString))
{
users = users.Where(s => s.LastName.ToUpper().Contains(searchString.ToUpper())
|| s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_desc":
users = users.OrderByDescending(s => s.LastName);
break;
case "Date":
users = users.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
users = users.OrderByDescending(s => s.EnrollmentDate);
break;
default: // Name ascending
users = users.OrderBy(s => s.LastName);
break;
}

int pageSize = 3;
int pageNumber = (page ?? 1);
return View(users.ToPagedList(pageNumber, pageSize));
}

Views\User\Index.cshtml

@model IEnumerable<RecreationalServicesTicketingSystem.Models.User>

@{
ViewBag.Title = "Users";
}

<h2>Users</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "User", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}

<table>
<tr>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder=ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>First Name
</th>
<th>
@Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.UserID }) |
@Html.ActionLink("Details", "Details", new { id=item.UserID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.UserID })
</td>
</tr>
}

</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
to
@Html.PagedListPager( Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter }) )

最佳答案

PageCount , PageNumber等是IPagedList<T>的属性, 不是 IEnumerable<T> .您需要更改 View 中的模型才能使用 IPagedList<T> , 并包含相关的 using 语句。

@using PagedList; // add
@using PagedList.Mvc; //add
@model IPagedList<RecreationalServicesTicketingSystem.Models.User> // change
@{
ViewBag.Title = "Users";
}
.....

关于c# - PagedList MVC 不包含 PagedListPager 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35589240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com