MVC4局部视图的JavaScript捆绑发行 [英] MVC4 partial view javascript bundling Issue

查看:123
本文介绍了MVC4局部视图的JavaScript捆绑发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目ASP.net 4,MVC4(RC)使用的Visual Studio 11。
我试图渲染呈现局部视图中MVC4的Razor视图。我有我的部分观点需要一些JavaScript。
在我的局部视图时,我有我的剧本段内的JavaScript如下它似乎并没有加载。

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. I have some javascript required for my partial view. On my partial view when I include my javascript inside the scripts section as follows it does not seem to load.

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

如果我删除脚本部分失败的jQuery的库(它们捆绑在一起,并呈现我的_Layout.cshtml页)尚未加载文档时准备code运行。

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.

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

_layout页code加载的jQuery库

_Layout Page code to load jquery libraries

@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!!

推荐答案

不要在局部包裹你的脚本在的document.ready

Don't wrap your script in a document.ready in the partial:

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

啊,不要把脚本谐音。 JavaScript的code应放置在单独的JavaScript文件。如果您要执行一些JavaScript时,部分被加载时,您只需外化这个脚本到一个可重用的功能/插件,你打电话,一旦你的部分被装入。

Ah, and don't put scripts in partials. Javascript code should be placed in separate javascript files. 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.

这篇关于MVC4局部视图的JavaScript捆绑发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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