1

I have a pretty intensive call to a Child Action, with a massive amount of c# code simply called in a few views by:

@Html.RenderPartial("mychildAction")

Now I need to load some javascript file whenever this action is called. I can not put the tags inline (inside the view) because Jquery (and other JS libraries) aren't loaded until the end of the html page.

The (master) _layout should be aware that this ChildAction was called. But it isn't because a Child Action gets a new HttpContext.

The only solution I came up with, is to switch to a partial (including a big chunk of c# code into the partial and add a few strings to some custom object kept in the HttpContext.Current.Items and make my _Layout act appropriatly). But doing so would mean I have no output caching on this ChildAction and I throw in bad practices like code in partials.

What is the best way to handle this scenario?

dampee
  • 3,392
  • 1
  • 21
  • 37
  • put jquery in the `` tag. I've seen others say that modern browsers are equipped to deal with this, now, and putting scripts at the end of the document is no longer increasing load speed. – ps2goat Mar 06 '14 at 23:51

3 Answers3

0

The View is responsible for rendering scripts and since the view knows when to render the partial view, it knows when to render the scripts for it.

In your layout you might find this:

@RenderSection("scripts", required: false)

The view can load scripts there like this:

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

This allows the calling view to render scripts.

What is not possible is to render scripts from a partial view or a sub-view.

You might find some workarounds on how to render scripts from a partial view, but in general is the responsibility of the view to know which scripts to load.

edit

Some of the workarounds I've seen are here.

Community
  • 1
  • 1
Odys
  • 8,951
  • 10
  • 69
  • 111
  • 1
    I don't think you can render sections from within a ChildAction ... (same for partial Views as you mentioned). Question still valid, how does the _Layout knows what scripts to load scripts if you use a partial. – dampee Mar 06 '14 at 23:56
  • @dampee the view will render scripts. That's the way to do it. – Odys Mar 06 '14 at 23:58
  • And then what is the best practice? What do you put in your method tagged with ChildActionOnly, or in the view which is called? – dampee Mar 07 '14 at 00:14
  • You should render scripts in the view. Remember you can create bundles and load child's stuff there. – Odys Mar 07 '14 at 00:16
  • Have you ever tried to get this to work with ChildActions? I couldn't get it to work with bundles & minification. If you add a reference to a bundle, the bundle will always include this JS, even on pages where the ChildAction is not referenced. In which View do you mean? Next to the @Html.Action or within the "partial" view of the ChildAction? – dampee Mar 07 '14 at 00:24
0

You can access the HttpContext from the calling action using

Html.ViewContext.ParentActionViewContext

So one could add something to the "Items" collection there, this approach could solve this particuliar issue, but I still wonder if it's the best approach.

dampee
  • 3,392
  • 1
  • 21
  • 37
-1

I agree with @Odys

_Layout.cshtml

@RenderSection("scripts", required: false)

ChildView

@{
    Layout = "~/Views/Share/_Layout.cshtml"
}
@section scripts
{
    <script></script>
}
francorobles
  • 41
  • 1
  • 7
  • You can not use sections is when you use Html.Action("method")! It's like trying to use sections within a Partial. – dampee Mar 07 '14 at 00:13