ASP.Net MVC 4 jQuery Accordion
In this tutorial, We will show you how to integrate Jquery Accordion into MVC project
Tools used
1. Microsoft VS 2012
2. Jquery
3. Jquery-ui
4. Jquery-ui.css
Reference
In this tutorial we will copy code from jQuery website and paste into MVC project.
Step to integrate – ASP.Net MVC 4 jQuery Accordion
- First of all, we will assume you know how to create a new MVC project in MS 2012, in case you need guide, please refer to here.
- After create the project, By default, jquery and jQuery-UI javascript files has been created in Scripts folder and you will see files like image below, We will need these files in later step.
.
- Download jQuery-ui.css and save into content folder.
[wp_ad_camp_6] - Create a controller named JqueryController. (Right click on Controllers Folder > Add > Controller)
- In the JqueryController.cs, add a view with name index. (Right click on ActionResult > Add View)
- Go to App_start>BundleConfig.cs, and modify your code to include the download jquery-ui.css
1 2 | bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css", "~/Content/jquery-ui.css")); |
Notes: By adding new line into bundle meaning that all view will download the file even though the page does not require the file, We assume that all of the views need this file, so that is why we include the file in bundleConfig.cs
7. In the Jquery/Index.chtml. Copy code from Jquery website and modify a little bit to make it look like MVC Code 😛
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | @Scripts.Render("~/bundles/jqueryui") <script> $(function () { $("#accordion").accordion(); }); </script> <div id="accordion"> <h3>Section 1</h3> <div> <p> Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate. </p> </div> <h3>Section 2</h3> <div> <p> Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna. </p> </div> <h3>Section 3</h3> <div> <p> Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui. </p> <ul> <li>List item one</li> <li>List item two</li> <li>List item three</li> </ul> </div> <h3>Section 4</h3> <div> <p> Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p> <p> Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p> </div> </div> |
- You need to make sure the jQuery javascript is place on top of jQuery-UI javascript file or else Accordion will not work. Modify _Layout.chtml, and it will look like this
@Scripts.Render("~/bundles/jquery")
- Run the application (F5). That’s all!