Asp.net-Mvc-3

在 MVC 3 Razor 視圖中找不到命名空間

  • October 24, 2016

我正在向我的視圖添加一個 PagedList 並鬆散地遵循本教程。我已經使用 Nuget 安裝了 PagedList 引用,如下設置我的控制器

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

並在頂部用以下模型寫了我的觀點

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

當我執行該頁面時,出現以下錯誤

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

PagedList dll 已正確載入到我的控制器中,因為當我將其從視圖中取出時,一切都按預期工作。CopyLocal 屬性設置為“True”,我嘗試在特定區域的 Views\Web.Config 中包含命名空間。我還能做些什麼來讓視圖看到命名空間?

我需要將命名空間添加到 Views web.config 文件中。

詳細資訊在此SO 文章中:

然後我需要關閉/重新打開 Visual Studio 2010 才能辨識它。編譯項目沒有幫助(web.config 可能只在項目載入時讀取一次)。

在使用未設置為“複製本地”的程序集時,我也遇到了這個問題,在更改程序集引用屬性後它按預期工作。

引用自:https://stackoverflow.com/questions/8313308