2011-11-04 73 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> 

而且,由于节是可选的(第二个参数=假)如果视图不提供任何内容,它将保持空白。