The website is not showing the AJAX response as expected

I am trying to implement a functionality in my view where the table expands into a detail row. Although the expand function is working and fetching the data successfully, I am facing an issue where the detail table that I intend to display is not appearing. Here is a snippet of the HTML code in the view:

<table>
<thead>
    <tr><td>Column 1</td></tr>
    <tr><td>Column 2</td></tr>
    <tr><td>Column 3</td></tr>
</thead>
<tbody>
   foreach(var data in ViewData["data"] as List<DataModel>)
    {
    <tr>
        <td>@data.Data1</td>
        <td>@data.Data2</td>
        <td>@data.Data3</td>
    </tr>
    <tr>
        <td class="hiddenRow" colspan="6">
            <div class="accordion-body collapse" id="@data.MembershipId">
                <div id="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7520051114011025141b10192a35111401145b381018171007061d1c053c11">[email protected]</a>" class="container">

                </div>
            </div>
        </td>
    </tr>           
    }      
</tbody>
</table>

Furthermore, I have implemented JQuery to use @data.MembershipId for retrieving the data for the detail table:

    $(document).ready(function () {
    var memberId;
    $('.header').click(function () {
        $(this).nextUntil('tr.header').slideToggle(50);
        memberId = $(this).data("target").replace("#", "");
        $.ajax({
            type: "GET",
            url: "GetActivity",
            data: { membershipId: memberId },
            success: OnSuccess,
            error: OnError
        });
    });
    function OnSuccess(data) {
        var TableContent = "<table>" +
            "<thead>" +
            "<tr>" +
            "<td>Detail Column 1</td>" +
            "<td>Detail Column 2</td>" +
            "<td>Detail Column 3</td>" +
            "<td>Detail Column 4</td>" +
            "<td>Detail Column 5</td>" +
            "</tr>" +
            "</thead>" +
            "<tbody>";
        for (var i = 0; i < data.length; i++) {
            TableContent += "<tr>" +
                "<td>" + data[i].data1 + "</td>" +
                "<td>" + data[i].data2 + "</td>" +
                "<td>" + data[i].data3 + "</td>" +
                "<td>" + data[i].data4 + "</td>" +
                "<td>" + data[i].data5 + "</td>" +
                "</tr>";
        }
        TableContent += "</tbody></table>";
        $("#UpdatePanel_" + memberId).html(TableContent);
    }
    function OnError(data) {
        alert("Error: " + data)
    }
});

Although the AJAX call to the Controller to fetch SQL data and retrieve the Data Model List seems to be functioning correctly, and the table is being built with the data upon inspection, it fails to display anything when expanding the table row.

I suspect this issue might not be related to the CSS, but I could be mistaken.

Answer №1

This is the view section

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut126</title>
    <style type="text/css">
        .customCSS {
            border: 3px solid #f00;
        }

        .header {
            border: 3px solid #4cff00;
        }
    </style>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        function Display(arg) {
            $("#" + arg.id).collapse('toggle');
        }
        $(function () {
            var memberIds;
            $('.header').click(function () {
                //header column look like column so we don't need all the children
                //$(this).nextUntil('tr.header').slideToggle(50);
                $(this).slideToggle(50);
                //memberId = $(this).data("target").replace("#", "");
                memberIds = $(".customCSS");
                var elements = [];
                memberIds.each(function () {
                    elements.push(this.id);
                });
                var stringOfElementIDs = elements.toString();
                $.ajax({
                    type: "GET",
                    url: '@Url.Action("GetActivity", "Home")',
                    data: { membershipIds: stringOfElementIDs },
                    success: OnSuccess,
                    error: OnError
                });
            });
            function OnSuccess(data) {
                var dataObj = JSON.parse(data);
                $.each(dataObj, function (i, anObj) {
                    var TableContent = "<table>" +
                   "<thead>" +
                   "<tr>" +
                   "<td>Detail Column 1</td>" +
                   "<td>Detail Column 2</td>" +
                   "<td>Detail Column 3</td>" +
                   "<td>Detail Column 4</td>" +
                   "<td>Detail Column 5</td>" +
                   "</tr>" +
                   "</thead>" +
                   "<tbody>";
                    TableContent += "<tr>";
                    $.each(anObj.outData, function (index, val) {
                        TableContent += "<td>" + val + "</td>";
                    });
                    TableContent += "</tr>";
                    TableContent += "</tbody></table>";
                    $("#" + anObj.id).html(TableContent);
                });
            }
            function OnError(data) {
                alert("Error: " + data)
            }
        })
    </script>
</head>
<body>
    <table>
        <thead>
            Click in the header to get activity
            <tr class="header">
                <td>Column 1</td>
                <td>Column 2</td>
                <td>Column 3</td>
            </tr>
        </thead>
        <tbody>
            @foreach (var data in ViewData["data"] as List<Testy20161006.Controllers.DataModel>)
            {
                <tr>
                    <td>@data.DataColumn1</td>
                    <td>@data.DataColumn2</td>
                    <td>@data.DataColumn3</td>
                </tr>
                <tr>
                    <td class="hiddenRow" colspan="6">
                        Click in the red row to expand
                        <div class="accordion-body collapse customCSS container" id="@data.MembershipId" onclick="Display(this)">
                            ...some text1...
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
</body>
</html>

Below is the code snippet

    public string GetActivity(string membershipIds)
    {
        List<ReturnValue> returnVals = new List<ReturnValue>();
        var listOfMembershipIds = membershipIds.Split(',');

        foreach (string id in listOfMembershipIds)
        {
            List<string> toArrayOut = new List<string>();
            toArrayOut.Add("Data1");
            toArrayOut.Add("Data2");
            toArrayOut.Add("Data3");
            toArrayOut.Add("Data4");
            toArrayOut.Add("Data5");
            ReturnValue returnVal = new ReturnValue { id = id, outData = toArrayOut };
            returnVals.Add(returnVal);
        }

        //Used the Newtonsoft.JSON library for serialization
        var serialized = JsonConvert.SerializeObject(returnVals);
        return serialized;
    }

    public ActionResult Tut126()
    {
        List<DataModel> listOfDataModel = new List<DataModel>();
        DataModel d1 = new DataModel { DataColumn1 = "Data101", DataColumn2 = "Data102", DataColumn3 = "103", MembershipId = "dataMembershipId1" };
        DataModel d2 = new DataModel { DataColumn1 = "Data201", DataColumn2 = "Data202", DataColumn3 = "203", MembershipId = "dataMembershipId2" };
        listOfDataModel.Add(d1);
        listOfDataModel.Add(d2);
        ViewData["data"] = listOfDataModel;

        return View();
    }

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

Building a Navigation Menu in Vue.JS Using Bootstrap Styles

Greetings! I had previously developed a Menu for my Vue 2 project. Upon creating a new Vue 2 project (with plans to migrate to Vue3), I proceeded to import my Menu, which consists of HTML and CSS. Unfortunately, it seems that the implementation is not wor ...

Executing a void C# web method using jQuery AJAX: A step-by-step guide

Could you enlighten me on the process of invoking this particular C# web method using AJAX and jQuery? public class Default { [WebMethod] public static void Example() { //perform action } } ...

The click event for a jQuery button is not functioning properly within a dataTable

When a new row is added to the dataTable, the edit and delete button clicks are not functioning. This issue arises after using ajax to add a new row. In the success function, I have destroyed the existing dataTable and then reloaded all the data to initiat ...

Struggling to update the inner HTML of a <p> tag using LocaleDateString() function

Recently, I've been experimenting with creating a calendar but encountered a frustrating issue. I created a variable storing a date using the toLocaleDateString method. However, when attempting to modify a paragraph's inner HTML, nothing seemed t ...

How do I utilize AJAX and HTML to invoke a RESTful web service in Java?

How to call a RESTful webservice in Java using HTML Ajax? I have created a simple webservice in Java and want to POST data into a database using HTML Ajax by calling a Java webservice in Eclipse. However, I encountered an error in my Java program. How can ...

Guide on displaying a table in Tabulator utilizing Flask alongside a JSON variable

As a beginner with Flask and JQuery, I'm seeking assistance. My goal is to display a dynamic table by linking the JSON parameter passed in render_template with Tabulator in my HTML page. Below is a snippet of my app.py: import os from flask import Fl ...

mapping buttons to images based on dynamic conditions

In my folder, I have over 1000 files. Using opendir, readdir, and is_dir, I can display the thumbnails of these files in my browser. Each thumbnail also has a corresponding button assigned to it. What I'm looking to do now is delete the 500th image wh ...

Content that is added dynamically seems to vanish once it is posted

Currently, I am working with an asp.net control (.ascx). I am dynamically adding rows to a table using jQuery in the following manner. $('#<%= doorFileNameTable.ClientID %>').after('<tr><td></td><tr/>').a ...

Problem with implementing AJAX Post functionality in back-end coding

I need assistance with sending JSON data via an Ajax method to a URL so that I can retrieve and use the values. While I believe I have most of the necessary components in place, I am unsure how to proceed once I reach my Web Method. Are there any issues pr ...

Summing Up Values in Jquery Loop Through Table Rows

I am facing a challenge with a table that contains a textfield for inputting numeric values. This textfield is part of a repeated row table, with the ID 'amount'. My goal is to calculate the sum of all values entered in the textfields with the ID ...

Guide to locating the index of a div element that has been dropped onto a droppable div

My web application features drag and drop functionality allowing users to drag a div and dynamically drop it onto a droppable area. Additionally, the droppable area has sortable functionality enabled. Now, I need to determine the indexing in order to acc ...

What is the best way to store article IDs using Javascript or HTML?

On my web page, I have a collection of links that users can interact with by clicking and leaving comments. These links are loaded through JSON, each with its unique identifier. But here's my dilemma - how can I determine which link has been clicked? ...

Using jQuery to extract data from an unnamed JSON array item

I am exploring ways to utilize JSON feeds and showcase them on PHP pages. While I have gained considerable knowledge from various examples, I am facing a challenge in dealing with arrays that do not have specific names or IDs for each field. For example: ...

Is it possible to substitute the variables with the function name?

Is there a way to duplicate the variable name? Take a look at the example below (note the name a1) $(document).ready(function() { name = "a1"; // This is "NAME" as a1 }) function name() { // USING "NAME" $("#test" + name).keydown(function(a) ...

jQuery JSON ajax response problem with knockout.js data binding

I've encountered an issue where a code example working with static JS-based JSON formatted data fails to work when the same data is parsed through Ajax. HTML CODE: <table> <thead> <tr><th>File Name</th>< ...

What is the method for showcasing login errors and prompting users to fill in all fields simultaneously on a page with PHP?

As I develop a PHP login page, I am implementing a system where users need to enter a username and password to access the platform. One query that arises is: How can I dynamically display an 'Invalid username or password' error message on the s ...

Position the text in the exact center of an HTML element using absolute positioning

Hey there! I am currently working on a website and I'm trying to create a simple delete button that can be used throughout the site. However, I'm having trouble getting it positioned correctly... Here's what I have so far: <button id="c ...

Using Ajax with MVC is easy with @Ajax helpers and JQuery's Ajax functionality

I have some knowledge of Ajax and I am currently learning MVC along with JQuery. I am curious to know if MVC Ajax.Helper and JQuery.Ajax are built on the same foundation. Are they similar to the standard Ajax method I learned using XMLHttpRequest xhr? If n ...

Ways to center text within a nested div in a parent div with table-cell styling

The issue I am facing involves a parent div with the CSS property display: table;. Nested inside is a child div with display: table-cell;. Despite applying text-align: center;, the text within the second div does not align to the center as expected. Here ...

Using varied colors to style list items

Is there a way to apply three different colors to my list items? What is the best method for achieving this? This is what I have in my code: li { width: 33.333%; float: left; padding: 15px; list-style: none; } .light { background-color: #0 ...