2011-11-04 90 views
2

我有佈局頁面和使用佈局的頁面。 如何在頭部不添加新的元素而不改變佈局(佈局已經包含頭部)?ASP.NET MVC佈局

佈局:

<head>...</head> 

我希望我的網頁如:

<head>all layout head logic... plus 
    my page new elements... 
    </head> 
+0

對不起,這個不清楚 – mozillanerd

回答

3

你可以在佈局中使用的部分。例如:

<html> 
<head> 
    @RenderSection("scripts", false) 
</head> 
<body> 
    @RenderBody() 
</body> 
</html> 

,然後在視圖中覆蓋此部分,併爲其提供內容:

@section scripts { 
    <script type="text/javascript"> 
     alert('hello'); 
    </script> 
} 

<div>Hello from the index view</div> 

而且,由於節是可選的(第二個參數=假)如果視圖不提供任何內容,它將保持空白。