使用 Razor 视图引擎将内容从部分视图 ASP.NET MVC 3 注入特定部分 [英] Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

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

问题描述

我在我的 _Layout.cshtml

中定义了这个部分

@RenderSection("脚本", false)

我可以从视图中轻松使用它:

@section 脚本 {@*东西来了*@}

我正在努力解决的是如何从局部视图中将一些内容注入到本节中.

假设这是我的查看页面:

@section 脚本 {<脚本>//代码来了}<div>便便吧便便

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

我需要在 _myPartial 部分视图的 Scripts 部分中注入一些内容.

我该怎么做?

解决方案

部分在部分视图中不起作用,这是设计使然.您可以使用一些自定义助手来实现类似的行为,但是老实说,包含必要的脚本是视图的责任,而不是部分的责任.我建议使用主视图的 @scripts 部分来做到这一点,而不必担心脚本.

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?

解决方案

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.

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

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆