1

I added jquery script in the layout page. but it is not accessible in the child view. I am not using any bundles.

Layout.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title </title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<h1>Test Apppp</h1>
@RenderBody()
 <script src="/Scripts/modernizr-2.7.2.js"></script>
    <script src="/Scripts/jquery.min.js"></script>
    <script src="/Scripts/bootstrap.min.js"></script>

</body>
</html>

MyView.cshtml

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

<div style="width: 100%; height: 500px;" class="float_left">

</div>
<script type="text/javascript">

    $(document).ready(function () {
        alert('dd');

    });   
</script>

Error shows in the $(document).ready(function){}); like $ is not defined Please suggest the solution

Thanks in advance

Razack
  • 950
  • 3
  • 13
  • 26
  • Possible duplicate [http://stackoverflow.com/a/15347957/3383479](http://stackoverflow.com/a/15347957/3383479) –  May 14 '14 at 07:05

2 Answers2

3

Use @RenderBody() and refer those scripts above the RenderBody()

<script src="/Scripts/modernizr-2.7.2.js"></script>
<script src="/Scripts/jquery.min.js"></script>
<script src="/Scripts/bootstrap.min.js"></script>
@RenderBody()
Madhu
  • 2,416
  • 3
  • 15
  • 33
1

Layout.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title </title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="~/Scripts/modernizr-2.7.2.js"></script>
    <script src="~/Scripts/jquery.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
</head>
<body>
 @RenderBody()
</body>
</html>

MyView.cshtml

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript" language="javascript">

    $(document).ready(function () {
        alert('dd');

    });   
</script>
<div style="width: 100%; height: 500px;" class="float_left">

</div>
malkam
  • 2,337
  • 1
  • 14
  • 17