1

Ive recently moved a project over to MVC4 and happily using script bundles to render scripts at end of the content.

The problem has arisen whereby some actions that are rendered (using Html.RenderAction) require their own script - in this example some jQuery event handlers set once the document is ready.

Aside from moving the jQuery inclusion so that it's earlier is there an elegant way of registering some script (or file) so that it's rendered after the bundles.

e.g.

<body>
@{Html.RenderAction("myaction",pars..);}

@section scripts { 
    @Scripts.Render("~/bundles/myscriptbundlecontainingjquery")  
}

</body>

thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
sambomartin
  • 6,663
  • 7
  • 40
  • 64
  • Do I understand correctly that you want your partial view would be able to register some javascript files? – Abbas Amiri Nov 08 '13 at 07:52
  • in the end I user a derivitive of Ronnie's solution found here http://stackoverflow.com/questions/12379266/trigger-the-bundle-inclusion-from-partial-or-editor-templates-in-asp-net-mvc-4 – sambomartin Nov 08 '13 at 11:27
  • it means I can request script bundles from content rendered from actions or partials - you need to make sure you request all the script bundles needed in the partial / rendered action as you can't control order easily. because the list is distinct there are no duplicates. see https://github.com/ronnieoverby/MvcAssetManager/blob/master/MvcAssetManager/Infrastructure/AssetManager.cs – sambomartin Nov 08 '13 at 11:29

1 Answers1

0

Have you considered making a partial view for that action, and render the jQuery there? That way the jQuery will only render if you call that partial view

Martin Solev
  • 154
  • 8
  • that's really what it's doing, but the partial view (html return from action) uses jquery which isn't yet included on the page. now the obvious thing to do would be to render the jquery script bundle first, but i was hoping there would be a way of registering the script so that it can be rendered after the bundles (after the content of the page) – sambomartin Nov 07 '13 at 22:39