tags:

views:

670

answers:

1

Hi

I have a page which contains a html.RenderPartial, that renders an aspnet 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;
    });

I can see in firebug the Page (view) is loaded once, but the script above in the partial view is still called twice, why why?

A: 

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.

tvanfosson
Well when the page Index.aspx loads, the partial view test.ascx loads via <div id="testDialog"><% Html.RenderPartial("test");%>.</div>I then have a button to open the dialog, but just loading the index.aspx makes the getJSON begin called twice.
Rasmus Christensen
Just found out the $("#Dialog").dialog(.... causes the second call. This means one call for the Html.RenderPartial and one call for .dialog, hmmmmmm
Rasmus Christensen
Is the script inside the dialog DIV? If so, move it outside. I haven't looked into it, but it could be that the dialog widget is creating a clone of the DIV when it is invoked. Moving the script outside the actual DIV should avoid the script object being cloned as well.
tvanfosson