我有布局页面和使用布局的页面。 如何在不改变布局的情况下向头部添加一些新元素。(布局已包含头部)?
布局:
<head>...</head>
我希望我的页面是这样的:
<head>all layout head logic... plus
my page new elements...
</head>
最佳答案
您可以在布局中使用部分。例如:
<html>
<head>
@RenderSection("scripts", false)
</head>
<body>
@RenderBody()
</body>
</html>
然后在 View 中覆盖此部分并为其提供内容:
@section scripts {
<script type="text/javascript">
alert('hello');
</script>
}
<div>Hello from the index view</div>
由于该部分是可选的(第二个参数 = false),如果 View 未为其提供任何内容,它将保持为空。
关于asp.net-mvc-3 - ASP.NET MVC 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014609/