Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace not found in MVC 3 Razor view

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?

like image 946
PlTaylor Avatar asked Nov 29 '11 15:11

PlTaylor


People also ask

How do you add a namespace in razor view?

To import a namespace in Razor view, we use using statement as we write in C# class however it should be prefixed with @. Here we are importing namespace MVCTraining5. Utility namespace.

Which namespace is used by MVC for Razor view engine?

The namespace for Razor Engine is System. The Razor file extension is "cshtml" for the C# language. By default, Razor View Engine encodes html tags or scripts before it's being rendered to view that avoids Cross-Site Scripting attacks.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.


2 Answers

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

like image 67
John M Avatar answered Oct 22 '22 16:10

John M


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.

like image 27
Malcolm Avatar answered Oct 22 '22 17:10

Malcolm