Utilizing jQuery GetJSON twice for ASP.NET MVC partial view

I've encountered an issue with a page that includes a html.RenderPartial, responsible for rendering an ASP.NET MVC partial view.

The partial view serves as a jQuery dialog, opened from the same page it is rendered on.

The problem arises when attempting to load and store a variable within the partial view upon displaying the dialog. This particular data is essential for lookups while interacting within the dialog. However, I noticed that the jQuery getJson function inside the partialview is being called twice during the loading of the page containing the partial view. Why is this happening?

Here's a snippet of code from the partial view:

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

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

Upon inspecting in Firebug, it appears that the page (view) loads once, yet the script mentioned above within the partial view gets called multiple times. Any insights into why this might be occurring?

Answer №1

It appears that this content will be loaded upon the initial page load. Have you considered reloading the partial using AJAX when the dialog window pops up? If so, the script may run again during that time.

UPDATE: After reviewing your changes, it seems likely that the script tag is located within the DIV and that the DIV is duplicated when the dialog widget appears. Moving the script outside of the DIV used by the dialog should resolve the issue you are experiencing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Encountering an issue with AJAX file upload displaying an error message for

Before I showcase my code, allow me to explain my objective. My goal is to create a page that updates a user's details in the database using AJAX. Initially, I successfully achieved this task. Subsequently, I wanted to enhance the functionality by inc ...

Utilizing Classes to Display Input Value in a Div

I need to display the value of a text input in another element. Since there are multiple occurrences of this code on the page, I prefer using classes for selection rather than IDs. The issue I'm facing is that I am unable to select the h2 element usi ...

The background image is not appearing on the div as expected

I've been attempting to create a div and set a background image for it using jQuery, but I seem to be facing issues. When I try setting the background color to white, it works fine. Here's the code snippet: function appendToDom(poster) { ...

The render() method in a class does not support Jquery functionality, unlike the componentDidMount() method where it works effectively

After updating my packages to the latest versions, I encountered an error in my project. Previously, everything was working fine, but after upgrading Next.js to version 9 and jQuery to version 3.5, the $.map function stopped working as expected. When I tri ...

What is the process for exporting a chart into Excel?

My current challenge involves displaying data extracted from a database in the form of a bar chart and then exporting both the data and the chart as an image into an Excel file. While I have successfully displayed the bar chart, I am facing difficulties in ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

After my jQuery functions are executed, my CSS rules are loaded

Struggling with the communication between CSS and jQuery, I find myself torn. In CSS, my rules are often very specific, like this: div#container > div#contentLeft { //Code here } However, when I add jQuery to spice up my site, the CSS rules seem to ...

AJAX/PHP causing delays due to lag problems

I've been trying to implement an asynchronous call in my PHP script, but I keep running into the same issue: "Maximum call stack size exceeded." This is causing severe lag on my site and I suspect there might be a loop somewhere in my code that I just ...

Ensure that images are not displayed until fully loaded by utilizing the jQuery .load() function following the completion of Ajax

I have been using the ajaxify-html5.js script from this repository and it has been working flawlessly. However, I noticed that during the content loading process (especially around line 145: $content.html(contentHtml).ajaxify();), there is a brief moment w ...

The functionality of jQuery's .hide method is not effective in this specific scenario

HTML section <div class="navigation-bar"></div> Jquery & JavaScript section function hideUserDiv(){ $('.ask-user').hide(); } var ask = '<div id="ask-user" style="block;position:absolute;height:auto;bottom:0;top ...

Basic Jquery CSS style requests

Can you help me troubleshoot this issue? var hover = $('<img />').attr('src', hovers[i]).css('position', 'absolute') I'm having trouble getting th ...

How can you determine if a user has selected "Leave" from a JavaScript onbeforeunload dialog box?

I am currently working on an AngularJS application. Within this app, I have implemented code that prompts the user to confirm if they truly want to exit the application: window.addEventListener('beforeunload', function (e) { e.preventDefault ...

Manage responses from ajax submission

My settings form has a unique feature to prevent users from making changes by mistake. After completing the form, users click the save button to trigger a modal (using Bootstrap modals) asking for their password before processing the original form. If the ...

Tips for organizing divs once another div has been hidden using jquery

I am working towards implementing a live result filter feature. There are three filters available: Good fit, bad fit, and scheduled. When the "Good fit" filter is clicked, it should display panels with the class "good_fit_panel". Let's assume there ar ...

What could be the reason for the malfunction of my jquery loader?

I'm currently working on enhancing a jQuery script by adding a loading GIF to it. I came across this plugin that I've used successfully in the past, but for some reason, it's not triggering when I submit a form. Could it be misplaced in my c ...

What could be causing multiple ajax requests to not run concurrently?

I have encountered an issue with my nested AJAX functions - only the first request seems to be executed and I am unsure of the reason behind this behavior. To troubleshoot, I have included some alert messages to track progress. I am working within a Dja ...

What is the process for performing the "extract function" refactoring in JavaScript?

Are there any tools for extracting functions in JavaScript similar to the "extract function" refactoring feature available for Java and jQuery developers in Eclipse or Aptana? Or perhaps in another JavaScript/jQuery IDE? ...

What is the best way to retrieve the elements stored within the 'this' object I am currently manipulating?

How can I access the elements nested within the 'this' that I am currently operating on? Below is the HTML code that I am currently working with: <div class="expander" id="edu">educational qualifications <ul class="list"&g ...

When using jQuery autocomplete and selecting an option by pressing ENTER, it triggers a popup blocker response

In my Office add-in, I have implemented jquery UI autocomplete to provide users with a list of clickable links. When a selection is made from the autocomplete dropdown, it triggers window.open to open the link in a new tab in the default browser. The fun ...

struggling to develop a sophisticated 'shopping cart organization' program

I am in the process of creating a database for video spots, where users can view and modify a list of spots. I am currently working on implementing a cart system that automatically stores checked spot IDs as cookies, allowing users to browse multiple pages ...