gpt4 book ai didi

asp.net-mvc-3 - ASP.NET MVC 布局

转载 作者:行者123 更新时间:2023-12-04 06:08:54 26 4
gpt4 key购买 nike

我有布局页面和使用布局的页面。
如何在不改变布局的情况下向头部添加一些新元素。(布局已经包含头部)?

布局:

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com