PagedList MVC不包含PagedListPager的定义

时间:2016-02-23 22:08:37

标签: c# asp.net-mvc asp.net-mvc-4 nuget-package

我不知道为什么PageCount,PageNumber和PageListPager不包含定义或无法找到。它询问我是否错过了使用指令或程序集引用,但我在用户控制器中有它。

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

控制器\ 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));
        }

视图\用户\ 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 }) )

3 个答案:

答案 0 :(得分:7)

PageCountPageNumber等属性为IPagedList<T>,而不是IEnumerable<T>。您需要在视图中更改模型以使用IPagedList<T>,并包含相关的using语句。

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

答案 1 :(得分:1)

正如其他人告诉你的那样,你错过了使用 PagedList PagedList.Mvc

我可以添加的内容,更好地在文件 Views / Web.Config 中添加命名空间。这将允许您在所有视图中使用名称空间 PagedList PagedList.Mvc ,并缩短您的代码。例如:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory" />
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Optimization"/>
      <add namespace="System.Web.Routing" />
      <add namespace="PagedList" />        <!--Add this line-->
      <add namespace="PagedList.MVC" />    <!--Add this line-->
    </namespaces>
  </pages>
</system.web.webPages.razor>

不再需要在所有人的视图/ cshtml中使用使用

@* @using PagedList; // No more need of this, can delete this line *@
@* @using PagedList.Mvc; // No more need of this, can delete this line *@
@model IPagedList<RecreationalServicesTicketingSystem.Models.User> // change
@{
    ViewBag.Title = "Users";
}
..... 

这是特别有价值的,当你有一个大项目时,它会缩短你的代码。

答案 2 :(得分:0)

您不必添加@using PagedList.Mvc,而@model不必在PagedList.IPagedList指令之前。而是只使用IPagedList而不是@model PagedList.IPagedList<RecreationalServicesTicketingSystem.Models.User> @using PagedList.Mvc

DisplayNameFor

节省奖金的时间:如果您在视图中使用@Html.DisplayNameFor(model => model.CreatedDateTime)(例如@Html.DisplayNameFor(model => model.FirstOrDefault().CreatedDateTime) ,则希望将其更改为:

.FirstOrDefault()

(添加了{IPagedList,因为DECLARE @spName varchar(64) = OBJECT_SCHEMA_NAME(@@PROCID) + '.' + OBJECT_NAME(@@PROCID); EXEC sp_LogSP_Save @spName; 更改了语义。请参见Using @Html.DisplayNameFor() with PagedList)。