简体   繁体   English

MVC4局部视图javascript捆绑问题

[英]MVC4 partial view javascript bundling Issue

I am working on a project ASP.net 4, MVC4(RC) using visual studio 11. I am trying to render an MVC4 Razor view that renders a partial view. 我正在使用visual studio 11开发一个项目ASP.net 4,MVC4(RC)。我正在尝试渲染一个呈现局部视图的MVC4 Razor视图。 I have some javascript required for my partial view. 我的部分视图需要一些JavaScript。 On my partial view when I include my javascript inside the scripts section as follows it does not seem to load. 在我的部分视图中,当我在脚本部分中包含我的javascript,如下所示它似乎没有加载。

@section Scripts {
    <script type="text/javascript">
        $(function () {
            alert("test");
        });
    </script>
}

If I remove the scripts section it fails as the jquery libraries (which are bundled and rendered on my _Layout.cshtml page) have not yet loaded when the document ready code runs. 如果我删除脚本部分它失败,因为当文档就绪代码运行时,jquery库(在我的_Layout.cshtml页面上捆绑并呈现)尚未加载。

<script type="text/javascript">
    $(function () {
        alert("test");
    });
</script>

_Layout Page code to load jquery libraries _Layout Page代码加载jquery库

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)

Does anyone know of a solution for this, or am I simply going about it the wrong way? 有没有人知道这个解决方案,还是我只是错误的方式? Its wrecking my head!! 它破坏了我的头!

Don't wrap your script in a document.ready in the partial: 不要将您的脚本包装在partial.ready中的document.ready中:

@section Scripts {
    <script type="text/javascript">
        alert("test");
    </script>
}

Ah, and don't put scripts in partials. 啊,不要把脚本放在局部。 Javascript code should be placed in separate javascript files. Javascript代码应放在单独的javascript文件中。 If you want to execute some javascript when a partial is loaded, you simply externalize this script into a reusable function/plugin that you call once your partial is loaded. 如果你想在加载部分时执行一些javascript,你只需将这个脚本外部化为一个可重用的函数/插件,你可以在部分加载后调用它。

Finally got this to work. 终于有了这个工作。 I removed my javascript from my partial view and placed it in the parent view (which is not partial), in the script section.This scripts section was created automatically when creating the view with scaffolding (Create) and was placed at the end of the page. 我从部分视图中删除了我的javascript并将其放在脚本部分的父视图(不是部分的)中。这个脚本部分是在创建带有脚手架(Create)的视图时自动创建的,并且放在了页。 To get this to work I had to move it to the top of the page - before the call to render my partial. 为了实现这个功能,我必须将它移到页面顶部 - 在调用渲染部分之前。

ClientDependency solves exactly this problem and allows you to add script references to partial views that get rolled up and placed at the end of the page (or wherever you specify) for you. ClientDependency正好解决了这个问题,并允许您将脚本引用添加到部分视图中,这些部分视图将汇总并放置在页面末尾(或指定的任何位置)。 It also deals with bundling, versioning and minification page by page. 它还逐页处理捆绑,版本控制和缩小。

The overhead of ensuring the script reference is on the "parent" View rather than the partial doesn't really bother me, but Client Dependency could be helpful if you had loads of partials all requiring their own script and CSS. 确保脚本引用的开销是在“父”视图而不是部分视图上并没有真正打扰我,但如果你有大量的部分需要他们自己的脚本和CSS,那么Client Dependency可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM