简体   繁体   English

jQuery GetJSON两次调用ASP.NET MVC部分视图

[英]jQuery GetJSON called twice for ASP.NET MVC partial view

I have a page which contains a html.RenderPartial, that renders an ASP.NET MVC partial view. 我有一个包含html.RenderPartial的页面,该页面呈现ASP.NET MVC部分视图。

The partial view is used as a jQuery dialog and is opend from the page where it is rendered. 局部视图用作jQuery对话框,并从呈现该视图的页面中打开。

The problem is that inside the partial view I want to load and store a variable when the dialog is displayed. 问题在于,在显示对话框时,我想在局部视图中加载并存储变量。 This data is used for some lookup while working inside the dialog. 在对话框内部工作时,此数据用于进行一些查找。 But when the page with the partial view loads, the jQuery getJson inside the partialview gets called twice. 但是,当加载具有部分视图的页面时, getJson的jQuery getJson会被调用两次。 Why? 为什么?

The code inside the partial view looks like this: 部分视图中的代码如下所示:

<script type="text/javascript">
$(function() {
    var groups = null;    

    $.getJSON("/RessourceGroup/List", null, function(data) {
        groups = data;
    });

In Firebug I can see the page (view) is loaded once, but the script above in the partial view is still called twice. 在Firebug中,我可以看到页面(视图)被加载了一次,但是部分视图中的上面的脚本仍然被调用了两次。 Why? 为什么?

This looks like it will get loaded when the page loads. 看起来页面加载时将被加载。 Are you also reloading the partial via AJAX when the dialog pops up? 对话框弹出时,您是否还要通过AJAX重新加载部分文件? If so, then it will probably run again then. 如果是这样,那么它可能会再次运行。

EDIT : Based on your update, I suspect that the script tag is inside the DIV and that the DIV is being cloned when the dialog widget is invoked. 编辑 :根据您的更新,我怀疑脚本标记在DIV内,并且在调用对话框窗口小部件时正在克隆DIV。 Moving the script outside the DIV that the dialog is using, should solve your problem. 将脚本移到对话框使用的DIV之外,应该可以解决您的问题。

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

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