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. Why?
问题是在部分视图中我想在显示对话框时加载和存储变量。在对话框内工作时,此数据用于某些查找。但是当加载部分视图的页面时,部分视图中的jQuery getJson会被调用两次。为什么?
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. Why?
在Firebug中,我可以看到页面(视图)被加载一次,但部分视图中的上述脚本仍然被调用两次。为什么?
1 个解决方案
#1
This looks like it will get loaded when the page loads. Are you also reloading the partial via AJAX when the dialog pops up? If so, then it will probably run again then.
这似乎会在页面加载时加载。当弹出对话框时,你是否也通过AJAX重新加载部分?如果是这样,那么它可能会再次运行。
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. Moving the script outside the DIV that the dialog is using, should solve your problem.
编辑:根据您的更新,我怀疑脚本标记在DIV内部,并且在调用对话框窗口小部件时正在克隆DIV。将脚本移动到对话框正在使用的DIV之外,应该可以解决您的问题。
#1
This looks like it will get loaded when the page loads. Are you also reloading the partial via AJAX when the dialog pops up? If so, then it will probably run again then.
这似乎会在页面加载时加载。当弹出对话框时,你是否也通过AJAX重新加载部分?如果是这样,那么它可能会再次运行。
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. Moving the script outside the DIV that the dialog is using, should solve your problem.
编辑:根据您的更新,我怀疑脚本标记在DIV内部,并且在调用对话框窗口小部件时正在克隆DIV。将脚本移动到对话框正在使用的DIV之外,应该可以解决您的问题。