Search code examples
javascriptc#asp.net-mvcrazordatatable

Using DataTables Plugin in Razor View - Cannot set properties of undefined (setting '_DT_CellIndex') Error


I am using a asp.net mvc web app. I have created a table and now I am wanting to use the plugin in datatables. I saw you only needed 3 lines of code to make this work. I attempted to do exactly what it required in order for it to work and give me the desired datatable, but it does not give me the table I am expecting. I even went to examples -> HTML (DOM) source data -> and under Javascript tab I entered the lines required.

Is there something I am doing incorrect here with the script tags or is the plug in just not working? I do not have the files downloaded and imported to my project.

Expecting a standard look like this enter image description here

But am getting this... so I am missing the look of the datatable plugin and the following Error.

Cannot set properties of undefined (setting '_DT_CellIndex')

my view:

@model List<Fright>


@{
    ViewData["Title"] = "Home Page";
     var result = Model.GroupBy(gb => gb.Value);
}

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <link href="//cdn.datatables.net/1.11.2/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.11.2/js/jquery.dataTables.min.js" defer></script>
    <script>
        $(document).ready(function () {
            $('#homeTable').DataTable();
        });
    </script>
</head>
<body>
    <div>
        <table id="homeTable" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Value</th>
                    <th>Option</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in result)
                {
                    var url = "/frightSummary/SummaryIndex?id=" + item.Key;
                    <tr>
                        <td>
                            <a href=@url>@item.Key</a>
                        </td>
                    </tr>

                }
            </tbody>
        </table>
    </div>
</body>
</html>

Solution

  • Your problem is a mismatch in the number of <td> </td>. You are just rendering 1 <td> in foreach loop but you have two <th></th> in the table header

    @model List<Fright>
    
    
    @{
        ViewData["Title"] = "Home Page";
         var result = Model.GroupBy(gb => gb.Value);
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
        <link href="//cdn.datatables.net/1.11.2/css/jquery.dataTables.min.css" rel="stylesheet"/>
        <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.11.2/js/jquery.dataTables.min.js" defer></script>
        <script>
            $(document).ready(function () {
                $('#homeTable').DataTable();
            });
        </script>
    </head>
    <body>
        <div>
            <table id="homeTable" class="display" style="width:100%">
                <thead>
                    <tr>
                        <th>Value</th>
                        <th>Option</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in result)
                    {
                        var url = "/frightSummary/SummaryIndex?id=" + item.Key;
                        <tr>
                            <td>
                                <a href=@url>@item.Key</a>
                            </td>
                        <td>
                                <a href=@url>@item.Option</a> /* output you Option value*/
                            </td>
                        </tr>
    
                    }
                </tbody>
            </table>
        </div>
    </body>
    </html> ```