0

I'm loading some scripts in the _Layout.cshtml page at the bottom of it:

<script src="~/scripts/jquery-1.8.2.min.js"></script>
<script src="~/scripts/easing.js"></script>
<script src="~/scripts/bootstrap.js"></script>

@RenderSection("scripts", required: false)

but for some reason, it won't load the jquery file. The easing and bootstrap script files will be loaded though: enter image description here

If I exclude jquery from the _Layout.cshtml page, it will fail to load the easing.js file (which was loaded previously): enter image description here

I'm going nuts as I can't figure out what's going on. Does anyone ever encountered such behavior or am I doing something wrong?

Zubzob
  • 2,653
  • 4
  • 29
  • 44
  • I found this answer to be relevant... [link](http://stackoverflow.com/questions/18860508/why-jquery-doesnt-work-on-a-page-defined-in-layout) Use of '@section scripts' in child page. – CYoung Aug 03 '16 at 00:21

1 Answers1

2

It appears the parser is struggling with the first ~ in the list (looking at the generated path coming from Chrome.) It could be a markup error earlier in your layout file or something else entirely - hard to say with the info provided. Typically, we use the Url.Content() helper instead of the raw ~ string in our script and style tags without issue.

I would recommend updating your <script> tags with a little bit more information.

  1. Add the type to the script tag.
  2. Try using the @Url.Content() html helper to see if that helps render the correct link.

This would be the resulting tag

<script type="text/javascript" src="@Url.Content("~/scripts/jquery-1.8.2.min.js")"></script>
Tommy
  • 39,592
  • 10
  • 90
  • 121
  • Do you have jquery bundles in BundleConfig.cs under App_Start folder ? – Neel Dec 16 '13 at 21:25
  • @Neel - yes, we have started using the bundling options in MVC this year. However, that is a completely different call/helper to render out the bundle URL. – Tommy Dec 16 '13 at 21:26
  • @Tommy: Ok, this works. The Url.Content helper fixed it (regardless of whether i specified the type of the HTML script element or not). Could you possibly explain why? – Zubzob Dec 16 '13 at 21:28
  • 1
    Without seeing more info, I couldn't say. I do want to say that using ~/Path without the URL helper was an addition to one of the newer releases of the Razor view engine, so perhaps that is what is causing the issue. Or, like I said earlier, could be a bad HTML markup somewhere? Either way, Url.Content is the typical practice in including static content in an MVC view. – Tommy Dec 16 '13 at 21:31
  • By bad markup you mean unclosed HTML tags? If yes, I've double checked it and all looks nice. Those scripts are placed right before the body closing tag (

    ).

    – Zubzob Dec 16 '13 at 21:38
  • See this answer to more specifically address what I said about the razor engine and update to being able to process ~ natively - http://stackoverflow.com/a/12461981/130387 – Tommy Dec 16 '13 at 21:42