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

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" },
],
});
This discussion has been closed.
Answers
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 useconsole.log( @Html.Raw(Json.Encode(Model.dayTrendMoreThanFive)) );
to validate the data structure that is returned.Kevin