Razor MVC render section

Miomir Dancevic picture Miomir Dancevic · Dec 4, 2014 · Viewed 24.6k times · Source

I am using master layout of Razor MVC that have something like this

@RenderSection("scripts", required: false)

And have partival view

_partial.cshtml

That have this

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

And another partial in partial

_partial_inside_partial.cshtml

That also have

@section scripts
{
    <script>
       $('div').addClass('red');
    </script>
}

The problem i have this code inside partial, its load at he middle of the page, and jquery is at the bottom?

Answer

Ashley Lee picture Ashley Lee · Dec 4, 2014

Sections don't work the same way in partial views. In order to achieve what you're after, your going to have to move your scripts to a file, include an HTML helper, then call that HTML helper to render your scripts for each partial view that is loaded.

Use this as a reference: Using sections in Editor/Display templates