从局部视图ASP.NET MVC 3的Razor视图引擎注入到内容具体章节 [英] Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

查看:121
本文介绍了从局部视图ASP.NET MVC 3的Razor视图引擎注入到内容具体章节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下定义上我的一节 _Layout.cshtml

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>

我需要注入脚本里面的一些内容 _myPartial 局部视图部分。

你有任何想法如何?

推荐答案

部分没有局部视图的工作,这就是设计。您可以使用<一个href=\"http://stackoverflow.com/questions/5433531/using-sections-in-editor-display-templates/5433722#5433722\">some定制的助手来实现类似的行为,但老实说它的视图的责任,包括必要的脚本,而不是部分的责任。我会用主视图的@scripts部分做到这一点,没有谐音担心脚本推荐你。

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.

这篇关于从局部视图ASP.NET MVC 3的Razor视图引擎注入到内容具体章节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆