2

I have a problem with rendering JavaScript file in _Layout.cshtml.

@section Scripts {
    <script src="@Url.Content("~/Scripts/Custom/productsSuggests.js")"></script>
}

When I paste it to Index.cshtml (Home) it works, but only on this page. I need this script to work globally. I have partial view SearchBox in HomeViews catalog, and Controller Action in HomeController.

2
  • Scripts section is part of _Layout, you have to put this script in _Layout in head section of the document Commented Apr 14, 2016 at 12:37
  • Here you define a section called Scripts. Do you actually render it later with RenderSection? Are you sure you need a section in the first place given that you're putting this into layout? Commented Apr 14, 2016 at 12:44

2 Answers 2

3

Because you are in the _Layout.cshtml view, it is likely the top level view. A section is a placeholder in a parent view.

Instead of your current code, try

@Scripts.Render("~/Scripts/Custom/productsSuggests.js")
Sign up to request clarification or add additional context in comments.

Comments

2

In the Layout.cshtml you can use: @Scripts.Render("YOUR BUNDLES")

When will be add layout to another page this bundle will be global work.

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.