Loading data from @Html.Raw(Json.Encode(

Loading data from @Html.Raw(Json.Encode(

ITPITP Posts: 8Questions: 3Answers: 0
edited April 2020 in Free community support

I'm having alot of trouble getting my data to load into a table. I already have the data loaded in my model and can see the data when I debug it, but it's not wanting to load into the datatable. Any ideas?

**example of data from linq expression in my controller: **

[{"key":"Location1-Freezing","cnt":6},{"key":"Location2-Outage","cnt":10},{"key":"Location3-Feature","cnt":6},{"key":"Location4-Device","cnt":6},{"key":"Location5-OtherDevice","cnt":6}]

**Code examples: **

                    <table id="TrendTable" style="width:100%">
                        <thead>
                            <tr>
                                <th>key</th>
                                <th>cnt</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
$('#TrendTable').DataTable({ data: @Html.Raw(Json.Encode(Model.dayTrendMoreThanFive)), responsive: true, paging: false, info: false, ordering: false, searching: false, select: true, columns: [ { data: "key" }, { data: "cnt" }, ], });

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    The above looks like it should work assuming @Html.Raw(Json.Encode(Model.dayTrendMoreThanFive)) returns a Javascript array of objects. I'm not familiar with the @Html.Raw() method but it looks like Json.Encode() is returning a JSON string. Maybe you can use console.log( @Html.Raw(Json.Encode(Model.dayTrendMoreThanFive)) ); to validate the data structure that is returned.

    Kevin

This discussion has been closed.