9

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 }) )
TykiMikk
  • 1,058
  • 3
  • 15
  • 31
  • Have you included the `using` statements in your view? And you model needs to be `@model IPagedList` (not `IEnumerable`) –  Feb 23 '16 at 22:11
  • 1
    I've tried what you suggested and I got this error. Compiler Error Message: CS0246: The type or namespace name 'IPagedList' could not be found (are you missing a using directive or an assembly reference?) Line 32: Line 33: Line 34: public class _Page_Views_User_Index_cshtml : System.Web.Mvc.WebViewPage> { Line 35: Line 36: #line hidden – TykiMikk Feb 23 '16 at 22:23
  • 1
    You cannot just change the original question (and I have rolled back your changes). You need both `@using PagedList;` and `@using PagedList.Mvc;` –  Feb 23 '16 at 22:26
  • 1
    Compiler Error Message: CS0246: The type or namespace name 'IPagedList' could not be found (are you missing a using directive or an assembly reference?).Line 40: public class _Page_Views_User_Index_cshtml : System.Web.Mvc.WebViewPage> { – TykiMikk Feb 23 '16 at 22:28
  • The message is self explanatory - your missing the correct `using` statements. Did you add both as per my last comment. –  Feb 23 '16 at 22:31
  • 1
    Yes it works ! Thanks! I thought Visual Studio puts in using statements as defaults when i install packages to that solution. – TykiMikk Feb 23 '16 at 22:41

3 Answers3

11

PageCount, PageNumber etc are properties of IPagedList<T>, not IEnumerable<T>. You need to change the model in the view to use IPagedList<T>, and include the relevant using statements.

@using PagedList; // add
@using PagedList.Mvc; //add
@model IPagedList<RecreationalServicesTicketingSystem.Models.User> // change
@{
    ViewBag.Title = "Users";
}
.....
  • 1
    Can you explain what's the difference using IEnumerable and IPagedList?? – TykiMikk Feb 23 '16 at 22:53
  • [IEnumerable](https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx) is the base class in the .NET framework for enumerating collections. It does not contain any properties. [IPagedList](https://github.com/troygoode/PagedList/blob/master/src/PagedList/IPagedList.cs) is an interface in the `PagedList.Mvc` assembly that extends `IEnumerable` to add additional properties for `PageCount` etc. `IPagedList` is `IEnumerable` but `IEnumerable` is not `IPagedList` –  Feb 23 '16 at 23:00
  • What gives with removing the accept/vote? Are you still having problems? –  Feb 23 '16 at 23:01
  • 1
    I had multiple screens on the same webpage so I removed it and did it again to make sure I upvoted. Thanks for the explanation! – TykiMikk Feb 23 '16 at 23:04
  • No need for `;` with the `@using` in `cshtml`, and no need for `@using PagedList` either or order change, see my answer. – Csaba Toth May 18 '20 at 21:22
3

You missing using of PagedList and PagedList.Mvc , as others told you.

What I can add, better add using of namespaces in file Views/Web.Config instead. This will allow you to use namespaces PagedList and PagedList.Mvc in all Views, and shorten your code. Example:

<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>

And no more need to use using in everyone view/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";
}
..... 

This is especially valuable, when you have a big project, it will shorten your code.

TPAKTOPA
  • 2,462
  • 1
  • 19
  • 26
1

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).

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121