Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PagedList MVC does not contain a definition for PagedListPager

I don't know why PageCount,PageNumber and PageListPager doesn't contain a definintion or could not be found. It's asking if I'm missing a using directive or an assembly reference but I have it in my user 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 }) )

Note: IMAGES AND CODE BELOW ARE ONLY FOR REFERENCE

Database Error

Visual Studio error

Controllers\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 }) )
like image 939
TykiMikk Avatar asked Feb 23 '16 22:02

TykiMikk


1 Answers

You don't have to add @using PagedList and @using PagedList.Mvc don't have to precede the @model directive. Instead just use PagedList.IPagedList instead of just IPagedList.

@model PagedList.IPagedList<RecreationalServicesTicketingSystem.Models.User>
@using PagedList.Mvc

Bonus time save: if you were using DisplayNameFor in your view (like @Html.DisplayNameFor(model => model.CreatedDateTime), then you'd want to change that to:

@Html.DisplayNameFor(model => model.FirstOrDefault().CreatedDateTime)

(.FirstOrDefault() was added because IPagedList changes the semantics. See Using @Html.DisplayNameFor() with PagedList).

like image 105
Csaba Toth Avatar answered Oct 22 '22 11:10

Csaba Toth