0

I have a layout _Layout.cshtml and in the end of it I have:

        ...
        ...
        @RenderSection("mySection", required: false)
    </body>
</html>

At the end of my view I have:

@section mySection {
    ...
    ...
}

My view is using partial views. When I tried to add content to the mySection section - nothing happend. In my partial view:

@section mySection {
   ....
   .... 
}

The section content from the partial view didn't render. Are sections allowed in PartialViews? if not, why??

Naor
  • 23,465
  • 48
  • 152
  • 268
  • are you doing this into `webform`? – Viral Shah Sep 13 '12 at 07:37
  • Have you declared a @RenderSection("mySection", false) in your parent or layout page? – John Sep 13 '12 at 07:38
  • 2
    http://stackoverflow.com/questions/5981490/how-to-render-javascript-into-masterlayout-section-from-partial-view – AliRıza Adıyahşi Sep 13 '12 at 07:45
  • @AliRıza Adıyahşi: I saw that already. I wish to use section and not workaround. Why does sections are not allowed?? – Naor Sep 13 '12 at 07:50
  • 2
    This is the @Darin Dimitrov's explanations. he is one of mvc experts and MVP : `Sections don't work in partial views and that's by design. You may use some custom helpers to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend you using the @scripts section of the main view to do that and not have the partials worry about scripts.` – AliRıza Adıyahşi Sep 13 '12 at 07:58
  • @AliRıza Adıyahşi: I saw his explanation but I didn;t understand why this is not the partial's responsibility. Supose we have partial that has autocomplete field and this is the only place we use autocomplete. I would like to add the autocomplete functionallity indise the prtial. – Naor Sep 13 '12 at 08:24

1 Answers1

0

Had exactly this issue today but as @AliRıza Adıyahşi quotes @Darin Dimitrov its not possible to access the section of the layout view from a partial view. In my case the solution was to simply have the script on the layout page.

From my experience I have found it is easier to have one JS file that is minified rather than have inline scripts. Can even have seperate files to make dev more easy and then batch file them together with minify on deploy.

John
  • 3,512
  • 2
  • 36
  • 53