Patrick Patrick - 2 months ago 13x
Javascript Question

Load a different jQuery library from Layout in View for DataTables in MVC

I need to load the specific jQuery library version in a View where I will use the DataTables library.

The problem is that I use jQuery 3.1 for the all project and I include the reference in the _Layout View, but DataTables only allow:

Bootstrap's JavaScript requires jQuery version 1.9.1 or higher, but lower than version 3

How can I, if possible, load a different version of jQuery in that specific view?


Regarding DavidG comment, it seems that the message is based on the Bootstrap jQuery supported library that does not support the jQuery v3.

I am going to update to Bootstrap 3.3.7 to avoid this problem ("Added support for jQuery 3") and use jQuery 3.1.0, but the DataTables problem remains is this case.

EDIT2 (Solution):

I decided to follow Shyju's suggestion to keep everything simple and based in a single stable jQuery version, so I have downgraded jQuery to 2.2.4 (last v2 stable version) and I have keeped Bootstrap to the last version (v3.3.7).

DataTables version remains the same (1.10.12).


If you absolutely need to load different version of library for a specific page, you can do something like this.

In the specific action method which returns the view which has the data table, set a ViewBag item

public ActionResult List()
  ViewBag.NeedsSpecialJQuery = true;
  return View();

and in the Layout(_Layout.cshtml), check for this viewbag item and based on it's existence/value, conditionally load the jquery version you need.

    if (ViewBag.NeedsSpecialJQuery !=null&& ViewBag.NeedsSpecialJQuery )
        <script src="path to jquery 1.9.1 here"></script>
        <script src="path to other jqurey here"></script>

While this may solve your current problem, I strongly suggest you consider updating your jQuery version (& other libraries depending on jQuery) so that you need only a single version. It might be a little bit of work. But it is worth it. You do not need to look for workarounds like this

Another option is to use a seperate Layout file for the specific view