我有以下页面:
页面.cshtml:
<some html>
@foreach (var item in Model.OffersOutstanding)
{
@Html.Partial("PartialOfferAccept", item.AcceptForm)
}
</some html>
<script>
// all js here
</script>
/* end of file */
和 PartialView PartialOfferAccept.cshtml:
<some html>...</some html>
<script>
// script, specified for partial view
</script>
在结果 html 中它可以工作,但是混合了 js 和 html。我想将所有部分 View 脚本放在页面末尾。最好使用 RenderAction,但 RenderAction 仅允许用于布局。任何分离部分 View 内容并将其放置在页面上不同位置的方法。 附言。不提供将部分 View 分离为 2 个部分 View 的能力:)
最佳答案
事实证明,我的下面的答案显然是不可能设计的。不知道为什么会这样,但是以下问题提出了与您遇到的相同问题,并且它链接到 Nuget 包以启用以下功能。
Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine
旧答案
您应该使用@section 帮助器。在该部分 View 上,您只需执行以下操作:
@section Scripts {
<script>
//Partial view code here
</script>
}
然后在您的 _Layout.cshtml 上,页面底部应该有以下内容
@RenderSection("Scripts", false)
通过这种方式,您可以轻松管理所有脚本并将其放置在一个位置,并在每个 View 的基础上使用自定义脚本
关于javascript - 将 PartialView 的 javascript 放在页面上的特殊位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962178/