2017-07-03 39 views
-1

我試圖在我學習ASP.MVC時使用一個小模型,但是我收到錯誤'GalleryModel' is a type, which is not valid in the current context模型是一種類型,它在當前上下文中無效

這是我的看法之內我的模型應用:

@model = GalleryModel 

該視圖控制器中調用:

public class GalleryController : Controller 
    { 
     public ActionResult Gallery() 
     { 
      GalleryModel model = new GalleryModel() { 
       ImageDirectories = Directory.GetFiles(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\MyFolder\") 
      }; 
      return View(model); 
     } 
} 

和模型被簡單地定義爲:

namespace TestSite.Models.Gallery { 
    public class GalleryModel { 
     public string[] ImageDirectories { get; set; } 
    } 
} 

所有的模型都應該做的就是保存一個我傳入它的字符串列表,並允許我在t中循環它們他認爲渲染部分(這些字符串將成爲圖像的文件目錄)。在視圖本身中,當在Visual Studio中查看時,模型GalleryModel突出顯示爲綠色,表明它將其識別爲模型,但它不會使用它,而只是顯示上述錯誤。我已經在視圖的web.config中將<add namespace="TestSite.Models.Gallery"/>添加到名稱空間的<pages pageBaseType="System.Web.Mvc.WebViewPage">列表中,但這沒有幫助。

有沒有人知道我要去哪裏錯了?

+0

刪除'=':'@model GalleryModel' –

+0

哇,我不能相信我沒有發現這一點。謝謝,我應該發現這樣一個簡單的錯誤! –

回答

-1

嘗試沒有「=」:

@model GalleryModel 
+0

不要回答錯字問題。而是標記它們。 –

+0

請不要發佈只顯示印刷問題或缺少字符的答案。這些答案不可能幫助未來的訪問者,因爲它們是針對OP的代碼的。相反,按照[help/on-topic]標記或投票結束問題。 –

相關問題