title: .NET MVC - Enabling script sections for AJAX-returned partial views
body: <p>I would like to make use of the (script) section in ASP .NET MVCs, i.e :</p>
  <pre><code>@section script
  {
      &lt;script&gt;alert(&quot;hello&quot;);&lt;/script&gt;
  }
  </code></pre>
  <p>...but with <strong>partials views</strong> as well, regardless of wether they are used within a complete page or an AJAX-returned content.</p>
  <p>It seems like MVC is expecting me to return partial views directly when doing AJAX, by returning a <code>PartialViewResult</code>. But if I do this, the returned view does not have a parent layout, which is where the @RenderSection insctruction is supposed to be, which causes the scripts declared in the script sections not to be rendered.</p>
  <p>(By the way it's not a question about whether returning scripts through AJAX is correct, that's another subject)</p>
  <p>To fix this issue, I thought about adding the <code>@RenderSection</code> instruction in the partial view. But this would not work, because the partial view is not guaranteed to be the root view of the return content, but also because it would make a duplicated call to <code>@RenderSection</code>, were the partial used in a complete page.</p>
  <p>So my second solution is to use an &quot;empty&quot; layout for every AJAX calls that is supposed to return HTML content, with only two instructions in that layout : <code>@RenderBody</code> to render the partial view tree, and <code>@RenderSection</code> to render all the scripts declared in script sections within the partial views.</p>
  <p>This second solution seems to perfectly suit my needs. However, it seems like this is not doing AJAX the way MVC is expecting me to, so I am worried this idea causing me problems in the future that I can't foresee at this day.</p>
  <p>Would you care to give me your opinion on this ?</p>
  <p>Thank you.</p>