Make jQuery UI and Bootstrap 2 work together in MVC template

Make jQuery UI and Bootstrap 2 work together in MVC template

Rate This
  • Comments 0

In the VS2013 preview MVC5 template, we included Bootstrap and jQuery UI together in the same project. However, some themes of jQuery UI (1.8.24 in the project template) is conflicting with Bootstrap (version 2.3.1 in the project template), especially for button. We have seen at least one such question in our forum.

Let’s create a test project first to see the effect.

1. File->New Project, choose Templates->Web->ASP.NET Web Application, select MVC, click Create Project

2. Right click project and choose “Manage NuGet Packages”, go to “Updates” tab, and update “jQuery UI (Combined Library)” to the latest version (1.10.3 at the time of writing)

3. Change the App_Start/BundleConig.cs file to include the jQuery UI js file.

Code Snippet
  1. bundles.Add(newScriptBundle("~/bundles/jqueryUI").Include(
  2.                         "~/Scripts/jquery-ui-{version}.js"
  3.                         ));

4. Change the Views/Shared/_Layout.cshtml file to include the style and script bundles. Note we are letting jQuery UI script to overwrite bootstrap. (Will show difference later)

Code Snippet
  1. @Styles.Render("~/Content/css")
  2. @Styles.Render("~/Content/themes/base/css")
  3. @Scripts.Render("~/bundles/modernizr")

 

Code Snippet
  1. @Scripts.Render("~/bundles/jquery")
  2. @Scripts.Render("~/bundles/bootstrap")
  3. @Scripts.Render("~/bundles/jqueryUI")
  4. @RenderSection("scripts", required: false)

 

5. Add the following test code to the end of Views/Home/Index.cshtml file

Code Snippet
  1.  
  2. <divclass="row">
  3.     <buttonid="jQueryUIButton"class="btn btn-info">BootStrap and JQueryUI</button>
  4.     <buttonclass="btn btn-info">BootStrap only</button>
  5.     <buttonid="jQueryUIButton2">JQueryUI only</button>
  6.  
  7.     <inputid="jQueryUISubmitButton"class="btn btn-info"type="submit"value="Input BootStrap and JQueryUI"/>
  8.     <inputclass="btn btn-info"type="submit"value="Input BootStrap only"/>
  9.     <inputid="jQueryUISubmitButton2"type="submit"value="Input JQueryUI only"/>
  10.  
  11.     <aid="jQueryUIAnchor"class="btn btn-info"href="#">anchor Bootstrap and JQueryUI</a>
  12.     <aclass="btn btn-info"href="#">anchor Bootstrap only</a>
  13.     <aid="jQueryUIAnchor2"href="#">anchor JQueryUI only</a>
  14. </div>
  15.  
  16. @section Scripts {
  17.     <script>
  18.         $(function () {
  19.             $("#jQueryUIButton").button();
  20.             $("#jQueryUIButton2").button();
  21.             $("#jQueryUISubmitButton").button();
  22.             $("#jQueryUISubmitButton2").button();
  23.             $("#jQueryUIAnchor").button();
  24.             $("#jQueryUIAnchor2").button();
  25.         });
  26.     </script>
  27. }

 

6. Run it, you can see the fonts are very different between the “BootStrap and JqueryUI” elements and “BootStrap only” element.

clip_image002

7. The order of including the Bootstrap JavaScript file and the jQuery UI JavaScript file matters. If we change step 4’s script order to put Bootstrap after jQuery UI script:

Code Snippet
  1. @Scripts.Render("~/bundles/jquery")
  2. @Scripts.Render("~/bundles/jqueryUI")
  3. @Scripts.Render("~/bundles/bootstrap")
  4. @RenderSection("scripts", required: false)

Run it, you can see “anchor JQueryUI only” does not get changed to button.

clip_image003

jQuery UI Bootstrap” can be used to make step 6 results a little bit better.

8. Revert step 7, Go to http://addyosmani.github.io/jquery-ui-bootstrap/ and download the latest archive, unzip the archive, locate “css/custom-theme” folder. Drag and drop images folder and latest jquery-ui-*.custom.css and jquery.ui.*.ie.css files to the project Content folder in solution explorer.

clip_image004

9. Add the following bundle in App_Start/BundleConig.cs

Code Snippet
  1. bundles.Add(newStyleBundle("~/Content/jqueryUIBootstrapCSS").Include(
  2.       "~/Content/jquery-ui-1.10.0.custom.css",
  3.       "~/Content/jquery.ui.1.10.0.ie.css"
  4.   ));

10. Change the CSS include in Views/Shared/_Layout.cshtml:

Code Snippet
  1. @Styles.Render("~/Content/css")
  2. @Styles.Render("~/Content/jqueryUIBootstrapCSS")
  3. @*@Styles.Render("~/Content/themes/base/css")*@
  4. @Scripts.Render("~/bundles/modernizr")

 

11. Run it.

clip_image005

12. Do the same as step 7, we will get the exact same output as step 7, which is:

clip_image006

From the above experiment, you can decide which combination of jQueryUI, BootStrap you'd like to use in your web project.

I also tried Bootstrap 3 RC1 with jQuery UI 1.10.3 in a sample website and the result is the same:

clip_image008