Highcharts does not support dynamic data loading with AJAX

After clicking a button, I expect a chart to be created but it does not appear.

In my project there are 2 divs: container1 and container2. Container1 successfully renders a partialView in Razor which creates the chart. However, container2 is supposed to display a chart after clicking the Change Data button. When the button is clicked, an AJAX request is made to call an action that renders the same partialView as shown in container1, but the chart does not show up!

Where did I go wrong?

The action is called, the server responds, and there are no errors in the console. This is what happens after clicking the button:

https://i.sstatic.net/qwoZn.png

Index.cshtml (View)

<button id="changeDataButton">Change Data</button>

<div id="container1">
    @{
        Random random = new Random();
        Html.RenderPartial("~/Views/Shared/chartPartialView.cshtml", new ChartModel()
        {
            CategoriesStackBar = new List<string>() { "A", "B", "C" },
            Series = new List<Series>()
                {
                    new ColumnSeries()
                    {
                        Name = "A",
                        Data = new List<ColumnSeriesData>()
                        {
                            new ColumnSeriesData()
                            {
                                Y = random.Next(100),
                            },
                            new ColumnSeriesData()
                            {
                                Y = random.Next(100),
                            },
                            new ColumnSeriesData()
                            {
                                Y = random.Next(100),
                            },
                        },
                    }
                },
            Id = "chart",
        });
    }
</div>

<div id="container2">

</div>

@section scripts{
    <script src="~/Scripts/highcharts.js"></script>

    <script>
        $(document).ready(function () {
            $('#changeDataButton').click(function (event) {
                console.log('change data button clicked');
                $.ajax({
                    async: false,
                    url: '/Home/UpdateChart',
                    success: function (partialView) {
                        console.log("success");
                        console.log(partialView);
                        $('#container2').innerHTML = (partialView);
                    }
                })
            })
        });
    </script>
}

(Controller)

    public ActionResult UpdateChart()
    {
        Random random = new Random();
        PartialViewResult partialViewResult = PartialView("~/Views/Shared/chartPartialView.cshtml", new ChartModel()
        {
            CategoriesStackBar = new List<string>() { "A", "B", "C" },
            Series = new List<Series>()
                {
                    new ColumnSeries()
                    {
                        Name = "A",
                        Data = new List<ColumnSeriesData>()
                        {
                            new ColumnSeriesData()
                            {
                                Y = random.Next(100),
                            },
                            new ColumnSeriesData()
                            {
                                Y = random.Next(100),
                            },
                            new ColumnSeriesData()
                            {
                                Y = random.Next(100),
                            },
                        },
                        Id = "dynamicChart",
                    }
                }
        });
        return partialViewResult;
    }

Thank you for your assistance in advance.

Answer №1

To manipulate the contents of a jQuery object, you cannot simply use innerHTML. Instead, utilize the .html() method:

$('#container2').html(partialView);

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

Issue with the transmission of FormData and string data to PHP

Struggling to properly handle sending file data and string data together through ajax to PHP. Having trouble retrieving the correct string data from $_POST[] in PHP, resulting in only receiving the initial alphabet of a string instead of the specific produ ...

Tips for transferring data from Controller to View using IEnumerable Model in MVC

Currently, I have a view containing 1700 records that need to be paginated using ajax for lighter page loading. The paging functionality works smoothly, bringing in a new set of records based on the selected page. Issue: The problem arises when displaying ...

Having trouble with the initial tap not being recognized on your mobile browser?

When using mobile web browsers (specifically chrome and firefox on iOS), I am experiencing an issue where the hamburger menu does not trigger when tapped for the first time. For a simplified version of the HTML/CSS/JS code, you can check it out at: https ...

Transferring information using express

Currently, my Express server is up and running and it's set to send an HTML file from the public folder of my project. The issue arises when I try to initiate a request from a client script linked in this HTML file to send data back to the server. Des ...

Sending the result of an AJAX request to the view using C#

I am currently utilizing Ajax to trigger an action in the controller. The code looks something like this: $('#kitchen').change(function () { var selectedKitchen = $('#kitchen').val(); if (selectedKitchen != '') { ...

Having trouble generating an array for checkboxes using jQuery, AJAX, and PHP

I'm almost there, but there's something missing. I'm attempting to send variables from multiple checkboxes to a PHP page using jQuery. This is my code: <div id="students"> <div class="field"> <label> ...

What is the best way to retrieve the latest files from a Heroku application?

Having recently migrated my Discord Bot to Heroku, I faced a challenge with retrieving an updated file essential for code updates. I attempted using both the Git clone command and the Heroku slugs:download command with no success in obtaining the necessar ...

How can animations be disabled in Angular/Javascript?

I have been assigned the task of developing an Angular component for my company's applications that will include a toggle to disable all animations within the app for accessibility purposes. It is important to note that I am unable to go into each in ...

How do I set up a timing mechanism in a VSCode extension to automatically clear error messages after a specified duration?

I'm currently in the process of developing a VSCode extension, and ran into an issue where I need to display an error message if a certain condition is met. However, I want this error message to automatically disappear after a set amount of time (say, ...

Discover the versions of key libraries/modules within the webpack bundle

Is it possible to identify all the libraries, scripts, or modules included in a webpack bundle through the developer's console of a website that is already running, without access to its codebase? Additionally, is there a way to determine the version ...

Unreliable Raycasting with Three.js

I am attempting to identify clicks on my Plane mesh. I have established a raycaster using provided examples as instructions. Below is the code snippet: http://jsfiddle.net/BAR24/o24eexo4/2/ Clicks made below the marker line do not register, even if they ...

The ajax method for loading an xml file results in displaying the undefined message

When attempting to fetch content from an xml file using ajax, the page initially displays "undefined". However, upon refreshing the page, the content loads successfully. Take a look at my code snippet below: $.ajax({ type: "GET", url: "xm ...

Unexpected bug encountered while implementing redux

I received a warning from eslint while working with create-react-app. ./src/components/auth.js Line 24: Unexpected labeled statement no-labels Line 24: 'authenticated:' is defined but never used ...

Using Next-auth.js: Redirecting from getServerSideProps to a specific callback URL - How can it be done?

Hey, I've been working on implementing authentication with the NextAuth library in my Next.js application. While following the documentation, I encountered a situation that wasn't covered. I'm attempting to create a 'route guard' o ...

Guide on loading a PDF asset dynamically within an Angular application with the help of webpack

I am having trouble loading a PDF file into my Angular app, which is running on the webpack dev server. I am using the HTML <object> tag with the data attribute to achieve this. The issue arises because the PDF path is generated dynamically at runti ...

Step-by-step guide on inserting a variable into .doc() to create a new table

Recently, I created a SignIn page that uses variables to store data fetched with document.getElementByClassName. Now, I am facing an issue while trying to create a new document on Firebase using the person's name stored in a variable. Struggling with ...

One common issue popping up in Webpack logs is the error message "net::ERR_SSL_PROTOCOL_ERROR" caused by a call to sock

Using react on the front-end and .net core 3.1 on the back-end. Running webpack on localhost:8080 for client-side development. Configuring proxyToSpa in Startup.cs: applicationBuilder.UseSpa(spa => { spa.UseProxyTo ...

What is the method for creating an array of strings in VueJS?

In my VueJS and Vuetify project, I am working on creating a modal that allows users to input strings into a text field. What I aim to achieve is adding the inputted string to an array when the user clicks on create button. For example, if I enter 'inp ...

Shut down the angular modal

I am currently learning AngularJS and I find myself working on an application that utilizes both angular modal and jqGrid. (I understand this may not be the ideal setup, but for now, I have to make it work with both.) The content of my modal is loaded usi ...

Looking to replace a background image using javascript?

(apologies for any language mistakes) I have multiple divs with a common background image. I assigned them the same class and set the background image using CSS in style.css which worked perfectly fine. Now, I want to change the background image dynamical ...