0

I am working on this ASP.NET MVC project where I am trying to add a Layout to my View. I have some .js files in my Scripts folder in the project, which I want to load in my View so that the components, such as DatePicker, Validation can work. However, by adding the Layout to the View, I see that I am getting the following error Uncaught ReferenceError: $ is not defined.

I understand this as, jQuery is not available for the View. But, I have the jQuery file in the Scripts folder. When I remove the Layout and add references to the scripts in the View directly, in <script src="~/Scripts/jquery-3.4.1.min.js"></script> way, I am not getting the error, and the View is performing correctly with the necessary functionalities.

Code snippets:

Create.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
    <form method="post" action="~/Controller/Create" id="myForm">
        ...
    </form>
</div>

<script type="text/javascript">
    $.validator.setDefaults({ ignore: ":hidden:not(select)" })
    $(document).ready(function(){
        $('form').validate({
            ...
        });
    });
</script>

_Layout.cshtml

<!DOCTYPE html>
<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")

</head>
<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", new { area = "" }, 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>

        </footer>
    </div>

    @RenderSection("Scripts", false)
</body>
</html>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

Scripts folder:

Scripts folder

2 Answers 2

1

You should add the RenderSection("Scripts" below the Jquery and Bootstrap one

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
 @RenderSection("Scripts", false)

also add the javascript in de view inside the Scripts rendersection

@section Scripts { 

<script>..</script>
}
Sign up to request clarification or add additional context in comments.

4 Comments

On doing that, I get the following error: Uncaught TypeError: Cannot read property 'setDefaults' of undefined
That line is present in <script></script> in my View.
That means that $.validator is undefined. Did you include the js file for validation?
I would be helpful if you update your question with the changes you made based o my suggestions
0

in your _Layout.cshtml inside <body> include jQuery @Scripts.Render("~/bundles/jquery")

  <!DOCTYPE html>
<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")

</head>
<body>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

<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", new { area = "" }, 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>

    </footer>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.