I ran into jQuery loading issue in an ASP.NET MVC 5 project. I am trying to load a partial view, so I used
@ Html.Action ("GetView", "Home")
A partial view contains some jQuery functions. When it loads, it shows a jQuery message not defined, but jQuery works on the main page.
So, I tried two other methods to load a partial view, and there is no problem with jQuery with these methods
@Html.Partial("_viewname") @Ajax.BeginForm()
Home controller
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetView(string id = "") { return PartialView("_ViewName"); } }
_Layout.cshtml
<!DOCTYPE html> <html> <head> <title>@ViewBag.Title</title> <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no"> <link rel="shortcut icon" type="image/x-icon" href="favicon.ico"> @Scripts.Render("~/bundles/modernizr") @RenderSection("Head", false) </head> <body> <section > <div> @RenderBody() </div> <div> @RenderSection("Sidebar", false) </div> </section> @Scripts.Render("~/bundles/jquery") @RenderSection("Scripts", false) </body>
Parent Index.cshtml Page
@{ ViewBag.Title = "Admin"; } <div id="main_content"> @Html.Action("GetView", "Packages") </div>
Partial view of _view.cshtml
<div class="row"> <div class="col-sm-12"> <select> <option>1</option> <option>2</option> </select> </div> </div> <script type="text/javascript"> $(document).ready(function () { $("select").addClass("form-control"); }); </script>
I think there is a problem with jQuery loading, but I donβt understand why this happens with certain actions.
Please offer me a solution to this problem.