Injecting content into specific sections from a pa

2018-12-31 07:31发布

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?

22条回答
裙下三千臣
2楼-- · 2018-12-31 07:36

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

查看更多
只靠听说
3楼-- · 2018-12-31 07:36

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' } }); });
查看更多
高级女魔头
4楼-- · 2018-12-31 07:40

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.

查看更多
梦该遗忘
5楼-- · 2018-12-31 07:41

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:

@model KeyValuePair<WebPageBase, HtmlHelper>
@{
    Model.Key.DefineSection("SectionNameGoesHere", () =>
    {
        Model.Value.ViewContext.Writer.Write("Test");
    });
}

And in the page inserting the partial view...

@Html.Partial(new KeyValuePair<WebPageBase, HtmlHelper>(this, Html))

You can also use this technique to define the contents of a section programmatically in any class.

Enjoy!

查看更多
姐姐魅力值爆表
6楼-- · 2018-12-31 07:41

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);

查看更多
笑指拈花
7楼-- · 2018-12-31 07:42

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.

查看更多
登录 后发表回答