Razor MVC render section
Asked Answered
P

2

10

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?

Pleomorphism answered 4/12, 2014 at 9:42 Comment(11)
it could just be a typo here but you have no closing script tag?Hypogenous
May be because you don't have your <script> tag closed?Alinealinna
Sorry typo, i made just example, because my code is much bigger, this way somebody can help me only what i need, not to look all my codePleomorphism
Check the html being render in the main view - are the scripts included and in what order?Riba
All is rendered exect alert, it goes in middle of the, page it work like this, because it simple alert, but if there is maybe jquery at bottom it will not workPleomorphism
I have updated my answer, for better understandingPleomorphism
Well that's why it wont work. Its jquery code and jquery has not yet been loaded. Try wrapping it in $(document).ready()Riba
The problem is that that script is showed at the middle of the page, i dont waht that, i want to put down, at the bottom of the page?Pleomorphism
I doubt that you can use sections in PartialsImpress
Please try if you dont trust me :)Pleomorphism
#7556900Impress
D
8

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

Dawdle answered 4/12, 2014 at 14:18 Comment(0)
C
0

I would place the @RenderSection("scripts", required: false) within the _layout.chtml head node as the last line. I know many people will disagree with me but I do it and so far I haven't got any issue by doing it. I use the "@RenderSection("scripts")" to place there my individual pages scripts. Some says it will increase or decrease the loading speed.

Cutcherry answered 20/3, 2024 at 15:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.