-1

I have an ASP.NET MVC 5 web project that I am trying to use with DataTables.js. Despite seemingly following all instructions correctly, the project is still producing errors in the browser console. This is probably very simple, but as I've said, I have followed all written instructions I can find and then some. I have also tried installing Datatables via Nuget and using the local copies of the script with similar results.

This is my code:

@model Website.Models.Forms.Form1

@{
    ViewBag.Title = "Form1";
}

<head>
    <link href="https://cdn.datatables.net/2.2.2/css/dataTables.dataTables.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="https://cdn.datatables.net/2.2.2/js/dataTables.min.js"></script>
    <script>

        $(document).ready(function () {
            $('#myTable').DataTable(); // Correct initialization method
        });

    </script>
</head>

<body>
    <h2>Form1</h2>
    <table id="myTable" class="display">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Row 1 Data 1</td>
                <td>Row 1 Data 2</td>
            </tr>
            <tr>
                <td>Row 2 Data 1</td>
                <td>Row 2 Data 2</td>
            </tr>
        </tbody>
    </table>
</body>

Here are the errors shown:

screenshot of error messages from my browser console

8
  • Are the referenced JavaScript resources successfully loading in the browser's development tools? Commented Apr 23 at 13:08
  • I am able to visit the pages referenced, and the network tab claims that the resources are loading correctly. Commented Apr 23 at 13:12
  • In that case I'm not able to replicate the problem. Copying/pasting this exact code into an HTML file and opening that file successfully initializes DataTables for me. What happens when you perform a similar test, removing ASP.NET from the equation? Commented Apr 23 at 13:13
  • You are correct. Removing Asp.NET fixes the issue. However, I do sort of need the rest of my application to function, so that's not really an option. Commented Apr 23 at 13:15
  • What's the difference when testing in the ASP.NET page then? Is the resulting HTML different somehow? Are the requests for the JavaScript resources in any way different? Are their responses different? The code shown only loads a couple of resources and doesn't perform much logic. The next step here is to reduce this as much as possible while still demonstrating the problem and to find what that difference is. Commented Apr 23 at 13:20

1 Answer 1

0

It looks like there was some sort of JQuery version conflict in my code.

Removing the following line from _Layout.cshtml was able to resolve my issue:

@Scripts.Render("~/bundles/jquery")

Note that doing this will prevent JQuery from automatically loading on all of your pages, so you will need to add another way to load jQuery either to the relevant pages or to the layout. Experiment with a test page if you're encountering this issue. Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

This question appears to offer a better explanation of the solution: stackoverflow.com/questions/28442960/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.