I put <script>
blocks which use jQuery in the body of one (and only one) cshtml file which uses the template and they causes error because jQuery is not loaded yet.
What's the point to put the @Scripts.Render("~/bundles/jquery")
at the bottom of _Layout.cshtml file?
The bottom of the _Layout.cshtml
.
@RenderBody() <hr /> <footer> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html>
The following shows the generated source of the cshtml file.
<script> $(document).ready(function () { /// $ not defined. // ..... }); </script> <hr /> <footer> </footer> </div> <script src="/Scripts/jquery-1.10.2.js"></script> <script src="/Scripts/bootstrap.js"></script>
As you can see above ~/bundles/jqueryval is the virtual path of the bundle which combines the files specified in it. So later on when you see this: @section Scripts { @Scripts.Render("~/bundles/jqueryval") } The above will include the scripts bundled under that reference.
Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)
Bundling and Minification are two performance improvement techniques that improves the request load time of the application. Most of the current major browsers limit the number of simultaneous connections per hostname to six. It means that at a time, all the additional requests will be queued by the browser.
You can use sections :
in your layout :
... <script src="/Scripts/jquery-1.10.2.js"></script> <script src="/Scripts/bootstrap.js"></script> @RenderSection("scripts", required: false) ...
in yours cshtml :
@section scripts { <script> $(document).ready(function () { /// $ not defined. // ..... }); </script> }
Just enclose it inside section scripts
in .cshtml Page as shown.
@section scripts{ <script> $(document).ready(function () { // ..... }); </script> }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With