I have this section defined in my _Layout.cshtml
@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 {
<script>
//code comes here
</script>
}
<div>
poo bar poo
</div>
<div>
@Html.Partial("_myPartial")
</div>
I need to inject some content inside the Scripts
section from _myPartial
partial view.
How can I do this?
I had this problem and used this technique.
Its the best solution i found which is very flexible.
Also please vote here to add support for cumulative section declaration
I have just added this code on my partial view and solved the problem, though not very clean, it works. You have to make sure the the Ids of the objects you are rendering.
$(document).ready(function () { $("#Profile_ProfileID").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } }); $("#TitleID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } }); $("#CityID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } }); $("#GenderID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } }); $("#PackageID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } }); });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.
There is a way to insert sections in partial views, though it's not pretty. You need to have access to two variables from the parent View. Since part of your partial view's very purpose is to create that section, it makes sense to require these variables.
Here's what it looks like to insert a section in the partial view:
And in the page inserting the partial view...
You can also use this technique to define the contents of a section programmatically in any class.
Enjoy!
Well, I guess the other posters have provided you with a means to directly include an @section within your partial (by using 3rd party html helpers).
But, I reckon that, if your script is tightly coupled to your partial, just put your javascript directly inside an inline
<script>
tag within your partial and be done with it (just be careful of script duplication if you intend on using the partial more than once in a single view);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.