Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have following section defined on my _Layout.cshtml

@RenderSection("Scripts", false)

I can easily use it from a view as follows :

@section Scripts { 
    @*Stuff comes here*@
}

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

Let's assume following is my view page :

@section Scripts { 

    <script>
        //code comes here
    </script>
}

<div>
    poo bar poo
</div>

<div>
  @Html.Partial("_myPartial")
</div>

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

do you have any idea how?

share|improve this question
1  
for anyone coming to this later - there is a nuget package for handling this: nuget.org/packages/Forloop.HtmlHelpers – Russ Cam Jun 17 at 20:20
up vote 25 down vote accepted

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.

share|improve this answer
38  
But what if the script is very specific to the partial? Doesn't it make logical sense for it to be defined in the partial, and not the view? – Jez Oct 25 '12 at 14:57
2  
Why is it by design? – Shimmy Nov 26 '12 at 10:12
1  
@Shimmy, because IMHO it is an unnecessary feature. Scripts have nothing to do in partial views anyway. They belong to separate javascript files. It is the responsibility of the parent view hosting those partials to register all the necessary scripts. – Darin Dimitrov Nov 26 '12 at 11:59
7  
@Darin: I disagree. What about the DRY principle? I don't like to repeat myself, even if it's only script references. – fretje Dec 7 '12 at 15:56
3  
@fretje, everybody has the right to express his opinion on the topic. I respect yours. In my answer I've expressed mine and linked to an answer which would allow you to achieve this task. But I've also highlighted on what I would recommend and do for this situation. – Darin Dimitrov Dec 7 '12 at 16:41
show 8 more comments

The first solution I can think of, is to use ViewBag to store the values that must be rendered.

Onestly I never tried if this work from a partial view, but it should imo.

share|improve this answer

Following the unobtrusive principle, it's not quite required for "_myPartial" to inject content directly into scripts section. You could add those partial view scripts into separate .js file and reference them into @scripts section from parent view.

share|improve this answer
What would happen if partial view is not at all rendered in the page? Do we still reference those .js files in parent and make it overload? – Murali Feb 10 at 17:15
I like this solution. It's like, OH YEA duh – meffect Apr 18 at 15:37

This is quite a popular question, so I'll post my solution up.
I had the same problem and although it isn't ideal, I think it actually works quite well and doesn't make the partial dependant on the view.
My scenario was that an action was accessible by itself but also could be embedded into a a view - a google map.

In my _layout I have:

@RenderSection("body_scripts", false)

In my index view I have:

@Html.Partial("Clients")
@section body_scripts
{
    @Html.Partial("Clients_Scripts")
}

And in my clients view I have (all the map and assoc. html):

@section body_scripts
{
    @Html.Partial("Clients_Scripts")
}

That way, if the clients partial is used as embedded in the view, it doesn't add section from the clients partial, and if you access the clients view directly, it will add the section to the _layout

That lets me have everything separated - it's a solution that works quite well for me, others may have issues with it, but it does patch the "by design" hole.

share|improve this answer

There is a fundamental flaw in the way we think about web, especially when using MVC. The flaw is that JavaScript is somehow the view's responsibility. A view is a view, JavaScript (behavioral or otherwise) is JavaScript. In Silverlight and WPF's MVVM pattern we we're faced with "view first" or "model first". In MVC we should always try to reason from the model's standpoint and JavaScript is a part of this model in many ways.

I would suggest using the AMD pattern (I myself like RequireJS). Seperate your JavaScript in modules, define your functionality and hook into your html from JavaScript instead of relying on a view to load the JavaScript. This will clean up your code, seperate your concerns and make life easier all in one fell swoop.

share|improve this answer
For like two or three months or so, I'm using RequireJS and I don't think I'll ever develop another web application without RequireJS. – tugberk Jun 18 at 10:00
JavaScript can be the View responsibility as well. – Kelmen Jul 13 at 4:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.