Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulate a Razor Section From a Partial
    text
    copied!<p>My main motivation for trying to do this is to get Javascript that is only required by a partial at the bottom of the page with the rest of the Javascript and not in the middle of the page where the partial is rendered. </p> <p>Here's a simplified example of what I'm trying to do:</p> <p>Here is the layout with a Scripts section right before the body.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;@ViewBag.Title&lt;/title&gt; &lt;link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /&gt; &lt;/head&gt; &lt;body&gt; @RenderBody() &lt;script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"&gt;&lt;/script&gt; @RenderSection("Scripts", false) &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's an example view using this layout. </p> <pre><code>&lt;h2&gt;This is the view&lt;/h2&gt; @{Html.RenderPartial("_Partial");} @section Scripts { &lt;script type="text/javascript"&gt; alert("I'm a view."); &lt;/script&gt; } </code></pre> <p>And here's the partial being rendered from the view.</p> <pre><code>&lt;p&gt;This is the partial.&lt;/p&gt; @* this never makes it into the rendered page *@ @section Scripts { &lt;script type="text/javascript"&gt; alert("I'm a partial."); &lt;/script&gt; } </code></pre> <p>In this example, the markup specified in the view is placed into the section, but the markup from the partial is not. Is it possible to populate a section from a partial view with Razor? If not, what are some other methods of getting Javascript that's only needed by partials at the bottom of the page without including it globally?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload