如何在 ASP.NET MVC3 中从部分视图中包含 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4707982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include JavaScript from a Partial View in ASP.NET MVC3
提问by Jonathan Matheus
I would like to be able to provide a way for partial views to include JavaScript code / files at the bottom of a view. This would enable partial views to include any JavaScript files that they depend on. For instance, if I wanted to write a partial that needs to create a JQueryUI dialog, I would like to import the JQueryUI JavaScript file as well as add JavaScript code that renders the dialog.
我希望能够为部分视图提供一种在视图底部包含 JavaScript 代码/文件的方法。这将使部分视图能够包含它们所依赖的任何 JavaScript 文件。例如,如果我想编写一个需要创建 JQueryUI 对话框的部分,我想导入 JQueryUI JavaScript 文件并添加呈现对话框的 JavaScript 代码。
I'm currently writing this code in the parent view, which makes it kind of pointless to use a partial view.
我目前正在父视图中编写此代码,这使得使用局部视图变得毫无意义。
I understand that calling RenderPartial
multiple times would result in scripts being included multiple times. This is a solvable issue once I know how to actually include JavaScript into the main view from the partial view.
我知道RenderPartial
多次调用会导致脚本被多次包含。一旦我知道如何从局部视图将 JavaScript 实际包含到主视图中,这是一个可以解决的问题。
回答by RPM1984
Define ContentPlaceHolderin your MasterPage (ASPX) or Sectionin your Layout Page (Razor)
在 MasterPage (ASPX) 或布局页面 (Razor) 中的部分中定义ContentPlaceHolder
ASPX:
ASPX:
<body>
<!-- End of Body -->
<asp:ContentPlaceHolder ID="JavaScriptIncludes" runat="server" />
</body>
Razor:
剃刀:
<body>
<!-- End of Body -->
@RenderSection("JavaScriptIncludes", required: false)
</body>
Then in the Partial:
然后在部分:
ASPX:
ASPX:
<asp:Content ID="ExtraJs" ContentPlaceHolderID="JavaScriptIncludes" runat="server">
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")" />
</asp:Content>
Razor:
剃刀:
@section JavaScriptIncludes
{
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")" />
}
Also think about using a HTML Helper to render out the <script>
tags.
还要考虑使用 HTML Helper 来呈现<script>
标签。
回答by VeganHunter
Here is how you can have Partial View with JavaScript code that uses any library (even when libraries are loaded at the end of the page)
以下是使用任何库的 JavaScript 代码的局部视图(即使库是在页面末尾加载的)
In your partial viewadd:
在您的部分视图中添加:
@{
TempData["Script"] += "MyFunction();";
}
<script type="text/javascript">
function MyFunction() {
// you can call your library here, e.g. jquery:
$(function () {
});
}
</script>
In your _Layout.cshtmlpage add after your libraries included:
在您的_Layout.cshtml页面中,在包含的库之后添加:
@*LOAD YOUR LIBRARIES HERE (E.G. JQUERY) *@
@if (TempData["Script"] != null)
{
<script type="text/javascript">
@Html.Raw(TempData["Script"].ToString())
</script>
}
You can have multiple partial views attaching theirs functions to the same key TempData["Script"]. They will happily coexist if you keep adding the functions using +=operator:
你可以有多个局部视图将它们的函数附加到同一个键 TempData["Script"]。如果您继续使用+=运算符添加函数,它们将愉快地共存:
@{
TempData["Script"] += "AnotherFunction();";
}
回答by Vadim
You can include <script>
tags inside the <body>
tag so you can have them inside your partial view.
您可以在<script>
标签内包含标签,<body>
这样您就可以将它们放在您的局部视图中。