在ChildAction中使用@section将其他脚本添加到页面 [英] Using an @section inside a ChildAction to add additional scripts to a page
问题描述
我有一个MVC视图,它由一个主视图和一些通过 @ Html.Action
添加的其他内容组成.
I've got a MVC View which is made up of a main view and some additional content added via @Html.Action
.
在其他内容(这是 ChildOnlyAction
)中,我希望能够向页面添加一些JS,但是我想将其添加到 @RenderSection("Scripts")
在布局中定义的代码块.
In the additional content (which is a ChildOnlyAction
) I want to be able to add some JS to the page, but I want to add it to the @RenderSection("Scripts")
block which I've defined in the layout.
我的儿童行为的视图可以使用以下内容吗:
Can my Child Action's View use this:
@section Scripts {
//Add scripts
}
到目前为止,我还无法使其正常工作,所以如果不是这样,那将是替代方法吗?
So far I haven't been able to get it to work, so if it doesn't what would be an alternative approach?
推荐答案
部分内容在部分视图中不起作用.您可以结合使用我在此处说明的扩展方法: https://stackoverflow.com/a/9663249/29407
Sections do not work in partial views. You could use a conjunction of extension methods that I illustrated here: https://stackoverflow.com/a/9663249/29407
因此,在您的 _Layout.cshtml
中,您将位于某个位置:
So in your _Layout.cshtml
you will have at some location:
@Html.RegisteredScripts()
然后在您的部分中:
@{Html.RegisterScript("~/scripts/foo.js");}
这篇关于在ChildAction中使用@section将其他脚本添加到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!