1

Some of my views require jQueryUI, some do not.

Rather than adding logic to my _Layout.cshtml Razor view, what is the best way to conditionally add certain bundles.

2 Answers 2

3

Try to use the @section directive.

In your Layout.cshtml:

<head> @Scripts.Render("~/bundles/jquery") @RenderSection("CustomScripts", false) </head> 

Then somewhere in your View:

@section CustomScripts { @Scripts.Render("~/bundles/jquery.ui") @Scripts.Render("~/bundles/jquery.templates") } 

This way even if you set CustomScripts later in your view, it will always appear in the head tag.

Sign up to request clarification or add additional context in comments.

Comments

1

Just put the script link at the bottom of the views that need it?

@Scripts.Render("~/bundles/jqueryui") 

1 Comment

Except the calls would be in the wrong place, outside the header or not at the bottom of the page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.