在ChildAction中使用@section将其他脚本添加到页面 [英] Using an @section inside a ChildAction to add additional scripts to a page

查看:53
本文介绍了在ChildAction中使用@section将其他脚本添加到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆