9

I am adding a PagedList to my view and loosely following this Tutorial. I have installed the PagedList reference using Nuget, set up my controller as follows

public ViewResult Index(int page = 1)
    {
        List<Part> model = this.db.Parts.ToList();
        const int pageSize = 20;
        return View(model.ToPagedList(page, pageSize));
    }

And written my view with the following model at the top

@model PagedList.IPagedList<RIS.Models.Part>

When I run the page I get the following error

Compiler Error Message: CS0246: The type or namespace name 'PagedList' could not be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 27:     
Line 28:     
Line 29:     public class _Page_Areas_Parts_Views_Part_Index_cshtml : System.Web.Mvc.WebViewPage<PagedList.IPagedList<RIS.Models.Part>> {

The PagedList dll is being properly loaded in my controller because when I take it out of my view everything works as expected. The CopyLocal property is set to 'True' and I have tried including the namespace in the Views\Web.Config in my specific Area. What else can I do to make the View see the Namespace?

PlTaylor
  • 7,345
  • 11
  • 52
  • 94

10 Answers10

16

I needed to add the namespace to the Views web.config file.

Details are in this SO post:

I then needed to close/re-open Visual Studio 2010 in order for it to recognize. Compiling the project didn't help (the web.config might only be read once upon project load).

Community
  • 1
  • 1
John M
  • 14,338
  • 29
  • 91
  • 143
4

I had this problem as well when using an assembly that wasn't set to "Copy Local", after changing the assembly reference properties it works as expected.

Malcolm
  • 41
  • 1
2

In my case, according to the package manager console, my project had a previous reference to PagedList however it was not showing up in my project references in the Solution Explorer.

Solution for me was to use the package manager console to remove PagedList.MVC, then remove PagedList and then re-install them again like so:

  • uninstall-package PagedList.mvc
  • uninstall-package PagedList
  • install-package PagedList
  • install-package PagedList.mvc

After that everything was good.

Jamie
  • 21
  • 3
2

First sorry for my terrible English. In my case, I use the MVC5 in Visual Studio Community 2015 and I solved the problem this way:

First I delete the cache of Visual Studio and NuGet cache:

  • Visual studio - go to %LocalAppData% \Microsoft\WebsiteCache and delete all folder.
  • Nuget - here

After I use the package manager console to remove PagedList.MVC, then remove PagedList and then re-install them again like so:

  • uninstall-package PagedList.mvc
  • uninstall-package PagedList
  • install-package PagedList
  • install-package PagedList.mvc

I holpe that help

Community
  • 1
  • 1
  • I didn't feel the need of clearing cache and temporary files. Just uninstalling and installing worked. Thank you. – Umer Farooq Dec 16 '16 at 07:58
0

FYI for anyone else with the same problem as me here, it was stopping on the controller "using PagedList;" for me (which was correct), however the problem I had was in the view's web.config I had added the namespace reference "PageList" - which was spelled incorrectly! (Should have been PagedList - note the 'd').

m.t.bennett
  • 1,290
  • 16
  • 34
0

In my case, the error was complaining about PagedList.MVC and I had to also install:

not only PagedList.

Cristian Boariu
  • 9,603
  • 14
  • 91
  • 162
0

In my case, I have to configure it in .cshtml, .cs and in web.config

.cshtml

@model PagedList.IPageList<namespace.model>
@using PagedList.Mvc;

Web.Config

<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup> 

.cs

using PagedList;

Hope this helps

mercu
  • 121
  • 2
  • 16
0

I got exactly this error when I build my lib as an

Console Application (EXE)

instead of

Class Library (DLL)

The lib as an console application was loaded and runnable inside C#-backend, but failed in razor with CS0246.

Changed the the Output type from Console Application to Class Library fixed for me.

Alexander
  • 471
  • 4
  • 18
0

I have always had to import MvcPaging to my views in order to use PagedList. But I was using the library prior to Nuget so I don't know how the namespaces may have changed. In a VB.NET web forms view, I used <%@ Import Namespace="MvcPaging" %>

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Pete
  • 1,790
  • 2
  • 19
  • 31
  • In the object browser it looks like the Namespace is 'PagedList' and that is what the controller is compiling with. – PlTaylor Nov 29 '11 at 15:34
0

Something is messed up with my overall project. I created a new project and copied the important items over and it all works now. I am not sure at all what is wrong with my original project.

PlTaylor
  • 7,345
  • 11
  • 52
  • 94