weixin_33709219 2019-09-04 06:46 采纳率: 0%
浏览 48

Ajax表未显示

I am creating ajax table from database. my Controller returning list of object but table is not creating on the page rather it shows json string. Here is my ajax call and view:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

<h2>Real Time Values</h2>

<body>
<div>
    <table id="tbldata" class="tabledata">
        <tr>
            <th>Meter Number</th>
            <th>Current A</th>
            <th>Current B</th>
            <th>Current C</th>
            <th>Current N</th>
            <th>Current Avg</th>
        </tr>
    </table>

</div>
<script>
    $(function () {
        $.get(("ShowRealTimeValues", "RealTimeValues", function (data) {
            var row;
            $.each(data, function (i, v1) {
                row += "<tr><td>" + v1.MeterNumber + "</td><td>" + v1.Current_A + "</td><td>" + v1.Current_B + "</td><td>" + v1.Current_C + "</td><td>" + v1.Current_N + "</td><td>" + v1.Current_Avg + "</td></tr>"
            });
            $("#tbldata").append(row);
        }))
    })
</script>

 </body>

Here is my controller. My controller is returning list of object.

   public ActionResult ShowRealTimeValues()
    {
        DateTime time = DateTime.Now;
        var data = myDbContext.RealTimeValues.OrderByDescending(a => a.Time).Take(1).ToList();

        return Json(data, JsonRequestBehavior.AllowGet);
    }

Dont know why table is not creating. there is not any error in console.

  • 写回答

2条回答 默认 最新

  • elliott.david 2019-09-04 07:54
    关注

    You can not use session at the client-side. To manipulate the table you not even need to keep the data in session. A workaround can be

     $.ajax({
            url: '@Url.Action("ShowRealTimeValues", "RealTimeValues")',
            type: "GET",
            success: function (result) {
                var theTable="<table id='yourTableId' class='yourTableClass'>";
                var theTr="";
                for (var i = 0; i < result.length; i++) {
                    theTr = theTr + "<tr><td>" + result[i] + "</td></tr>";
                }
                theTable = theTable + theTr + "</table>";
                $(".tblContainer").empty();
                $(".tblContainer").html(theTable);
            }
            });
    
        <div class='tblContainer'>
    
        </div>
    
    评论

报告相同问题?