tugberk tugberk - 1 year ago 84
ASP.NET (C#) Question

Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

I have this section defined in my


@RenderSection("Scripts", false)

I can easily use it from a view:

@section Scripts {
@*Stuff comes here*@

What I'm struggling with is how to get some content injected inside this section from a partial view.

Let's assume this is my view page:

@section Scripts {

//code comes here

poo bar poo


I need to inject some content inside the
section from
partial view.

How can I do this?

Answer Source

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 using the @scripts section of the main view to do that and not have the partials worry about scripts.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download