JQuery GetJSON called twice for ASP.NET MVC partial view

asp.net-mvcjquerypartial-views

I have a page which contains a html.RenderPartial, that renders an ASP.NET MVC partial view.

The partial view is used as a jQuery dialog and is opend from the page where it is rendered.

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?

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?

Best Answer

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.

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.