【发布时间】:2009-11-20 03:21:56
【问题描述】:
如何注入脚本标签如
<script src="somejsfile"></script>
或
<script type="text/javascript>some javascript</script>
从局部视图进入页面的头部标签?
更新:回答旧问题 这是关于 ASP.NET MVC 的。我们可以使用 RenderSection。这里是使用 Razor 视图引擎的 MVC 3 示例:
布局视图或母版页:
<html>
<head>
<script ...></script>
<link .../>
@RenderSection("head")
</head>
<body>
...
@RenderBody()
...
</body>
</html>
查看,例如主页:
@section head{
<!-- Here is what you can inject the header -->
<script ...></script>
@MyClass.GenerateMoreScript()
}
<!-- Here is your home html where the @RenderBody() located in the layout. -->
【问题讨论】:
-
你可以在页面中加入
-
我意识到它并不一定必须放在那里,但如果我愿意的话,我正在寻找一种好的方法。
-
@vakman - 我的回答满足您的需求吗?