If I remove the scripts section it fails as the jquery libraries (which are bundled and rendered on my _Layout.cshtml page) have not yet loaded when the document ready code runs.
_Layout Page code to load jquery libraries
@Scripts.Render("~/bundles/jquery") @RenderSection("scripts", required: false)
Does anyone know of a solution for this, or am I simply going about it the wrong way? Its wrecking my head!!
Don't wrap your script in a
document.ready in the partial:
ClientDependency solves exactly this problem and allows you to add script references to partial views that get rolled up and placed at the end of the page (or wherever you specify) for you. It also deals with bundling, versioning and minification page by page.
The overhead of ensuring the script reference is on the "parent" View rather than the partial doesn't really bother me, but Client Dependency could be helpful if you had loads of partials all requiring their own script and CSS.
©2020 All rights reserved.