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?
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.
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With