Search code examples
javascriptasp.net-mvc-3orchardcms

Orchard - Including Javascript files in partial views


In Orchard, how do I include a javascript file in my partial view that's loaded from an ajax call?

Here's what I've tried:

@{Script.Include("ownerInfo.js");}

Orchard completely ignores this if the view is hit from an ajax call

Edit: The below works, although including scripts by hand is not ideal. See Piedone's example below for a better alternative.

<script type="text/javascript" src="./Modules/ModuleName/Scripts/script.js"></script>

Solution

  • I think this is by design. If you look at Orchard.UI.Resources.ResourceFilter (this is the filter adding those shapes to the layout that render static resources) it contains the following check:

    // should only run on a full view rendering result
            if (!(filterContext.Result is ViewResult))
                return;
    

    This prevents it from injecting the shapes into the layout if the action result is e.g. a PartialViewResult.

    You could do the following:

    • Insert the script "by hand"
    • Include the script with Orchard and display the head and/or foot scripts' shape in your view:

      Display.HeadScripts() Display.FootScripts()