Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys,

I've just created a new ASP.NET MVC application within Visual Studio 2013. But, I ammediately run into problems. When I try to use jQuery, i get the "JavaScript runtime error: '$' is undefined" error.

I didn't modify anything. The only thing I added was:

<pre lang="HTML">
  <script>
    alert($("#col-md-4").val());
    alert($(".col-md-4").val());
</script>


This is the body part of my layout file

HTML
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>© @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>


I've got the bundels in the "BundleConfig.cs" and I've got the jquery file's like "jquery-1.10.2.js" in the scripts folder. Which is all generated when created an ASP.NET MVC project.

Can anyone tell me what's wrong here?

Many thanks in advance!
Posted
Comments
Richard C Bishop 27-Feb-14 15:37pm    
Post the HTML that shows you referencing your jQuery files. There could be an order issue.

1 solution

Fixed it...

The jquery bundle had to be in the head not at the bottom of the body...

HTML
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
</head></head>
 
Share this answer
 
Comments
Richard C Bishop 28-Feb-14 15:29pm    
Good work!
[no name] 16-Jan-19 8:11am    
I have it in Head only. But i am getting this error
"Cannot perform runtime binding on a null reference"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900