Trying to populate datatable using javascript sourced data

Trying to populate datatable using javascript sourced data

DavidsonsDavidsons Posts: 2Questions: 1Answers: 0
edited December 2020 in Free community support
<table class="table" display="hidden" id="datatable">  </table>

@section scripts{
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.22/af-2.3.5/datatables.min.css" />

    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.22/af-2.3.5/datatables.min.js"></script>
    <script>
            
                           var m='@Html.Raw(Json.Encode(Model))';

        $(document).ready(function () {
            $("#datatable").DataTable({
                
                 
                
            });
        })
    </script>


}

**:
** error : Cannot read property 'aDataSort' of undefined TypeError**:
I am trying to populate datatable using javascript data. My model is of type List<Object>
The thing is i have tried the same using model type of datatable, have the same error
Can somebody help?
I am using asp.net mvc
:

Edited by Kevin:  Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

Answers

  • kthorngrenkthorngren Posts: 21,339Questions: 26Answers: 4,954

    It doesn't look like you are creating a thead in your table. You can add one or use columns.title for each column to create it.

    Kevin

  • DavidsonsDavidsons Posts: 2Questions: 1Answers: 0
    edited December 2020

    @kthorngren
    yes but the thing is its making an error because of the var m=@Html.Raw(Json.Encode(Model)); line, and i dont know why

  • yericusyericus Posts: 20Questions: 4Answers: 1
    edited December 2020

    var m='@Html.Raw(Json.Encode(Model))';

    If you put your variable in ' ... ' you define your variable as a a string no ?
    Maybe remove your ' ' to begin with ?

    var m= @Html.Raw(Json.Encode(Model));

This discussion has been closed.