2013-08-04 78 views
1

我的視圖找不到佈局。但一切都很好。我下面的結構如下所示:視圖找不到佈局頁面

enter image description here

而我的看法如下:

@model xxx.Web.Model.Home.IndexModel 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/_MainLayout.cshtml"; 
} 

我的佈局如下所示:

<!DOCTYPE html> 
<html lang="tr"> 
<head> 
<meta charset="utf-8"> 
<title>Bootstrappage:Free Bootstrap Template (bootstrap 2.3.1 version)</title> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<meta name="description" content=""> 
<meta name="author" content=""> 
<!-- 
<link rel="stylesheet" href="themes/style/bootstrap.min.css" /> 
<link rel="stylesheet" href="themes/style/bootstrap-responsive.min.css" /> 
--> 
<link rel="stylesheet/less" type="text/css" href="../../themes/less/bootstrap.less"> 
<script src="../../themes/js/less/less.js" type="text/javascript"></script> 
<link rel="shortcut icon" href="assets/ico/favicon.ico"> 
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png"> 
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png"> 
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png"> 
<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png"> 
<script type="text/javascript"> 
    $(function() { 
     @RenderSection("DocumentReady", false) 
    }); 
</script> 
</head> 
<body data-offset="40"> 
@{Html.RenderAction("Header", "Partial");} 
@{Html.RenderAction("Navbar", "Partial");} 
@RenderBody() 
@{Html.RenderAction("Footer", "Partial");} 
<script src="themes/js/lib/jquery-1.9.1.min.js"></script> 
<script src="bootstrap/js/bootstrap.min.js"></script> 

</body> 
</html> 

而且我的控制器如下圖所示:

public ActionResult Index() 
    { 
     IndexModel model = new IndexModel(); 
     HomeModelFactory modelFactory = new HomeModelFactory(); 
     model.Files = modelFactory.CreateIndexModel(); 

     return View(model); 
    } 

和例外: enter image description here

爲什麼我得到這個異常?它運作良好。

+0

最近有些事情明顯改變,突然停止工作。你能比較一個正在工作的版本嗎? –

+0

只有我在cshtml的頂部添加了「@model xxx.Web.Model.Home.IndexModel」。 – fuat

+1

您可以向我們展示_MainLayout.cshtml的內容嗎?我懷疑有一個你正在嘗試使用的變量是空的 – jzm

回答

2

還有視圖/控制器的全部內容嗎?

一些可能的場景沒有看視圖/控制器; - 有些部分缺失(如「DocumentReady」) - 視圖試圖解析未設置模特屬性(即空)

+0

控制器代碼添加到問題。 – fuat

+1

夥計們,我發現了這個問題。我的模型始終爲空,所以我得到例外。這不是關於佈局頁面。 – fuat

+1

太好了。如果模型爲null是業務邏輯的一部分,那麼您可能希望在視圖中添加一個空檢查並向用戶顯示錯誤(通用或特定錯誤),而不是冒着客戶端的空引用異常風險。 – Uchitha

-4

這是你的問題:Layout = "~/Views/_MainLayout.cshtml";

的_layout參考應該是Layout = "~/Views/Shared/_MainLayout.cshtml";

不要再犯這些類型的錯誤!

+0

這是不正確的。佈局視圖可以在任何地方。還嘗試過Layout =「〜/ Views/Shared/_MainLayout.cshtml」;.還是一樣。 – fuat

+0

'_MainLayout.cshtml'文件不在'/ shared'目錄中。仔細看一下目錄屏幕截圖 – NotMe

+0

這是誤導。我有同樣的問題,像這樣的回答沒有幫助。 – FrenkyB