2013-03-01 78 views
1

我是一位經驗豐富的Web開發人員,他一直在做項目管理而不是開發一年,所以我試着跳回來學習Razor。到目前爲止,這是一個令人沮喪的失敗。Razor RenderSection不起作用

我在VS2012中創建一個新的空剃刀的網站,並創建下列文件:

_MainLayout.cshtml:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Razor Test</title> 
    </head> 
    <body> 
     <div>@RenderBody()</div> 
     <div>@RenderSection("testSection")</div> 
    </body> 
</html> 

ContentPage1.cshtml:

@{ 
    Layout = "_MainLayout.cshtml"; 
} 

<div>This is the content on the Razor Test Page.</div> 

和TestSection。 cshtml:

@{ 
    Layout = "_MainLayout.cshtml"; 
} 

@section TestSection { 
    <h1>this is test section</h1> 
} 

當我嘗試和運行這個頁面,我得到以下錯誤:

Section not defined: "TestSection".

而且知道發生了什麼事?這應該是我可以得到的那麼簡單。顯然它簡單。

回答

1

Sections應該去你的頁面中沒有單獨CSHTML

ContentPage1.cshtml:

@{ 
    Layout = "_MainLayout.cshtml"; 
} 
@section testSection { 
    <h1>this is test section</h1> 
} 

<div>This is the content on the Razor Test Page.</div> 

或者,如果你想要,而不是顯示使用部分意見「像部分」單獨cshtml

+0

謝謝。我看過的例子都沒有說明這一點非常明確(或者我正在看部分視圖示例,但沒有認識到這一區別)。 – Klay 2013-03-01 15:10:44

+0

似乎節名稱和RenderSection值區分大小寫。 – Aaron 2014-05-11 03:29:48

相關問題