如何呈现JavaScript成从局部视图MasterLayout部分? [英] How to render JavaScript into MasterLayout section from partial view?

查看:110
本文介绍了如何呈现JavaScript成从局部视图MasterLayout部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于MVC3和剃刀引擎,我得到了

Given MVC3 and Razor engine, I got

_MasterLayout.cshtml

@RenderSection("JavaScript", required: false)
..
..
@RenderBody()
..

View.cshtml 与_MasterLayout.cshtml在_ViewStart.cshtml定义

View.cshtml with _MasterLayout.cshtml defined in _ViewStart.cshtml

..
@Html.RenderAction("PartialView", "PartialController")
..

PartialView.cshtml

..
@section JavaScript
{
........
}
..

我怎样才能确保从局部视图Java脚本在总纲部分结束了?谢谢你。

How can I make sure that Java Script from Partial View ends up in the Master Layout section? Thanks.

编辑:上面的场景不起作用,因为局部视图没有定义主布局。视图中,另一方面,确实有布局与定义RenderSection。如果我从局部视图移动部分Javascript功能来查阅,剃刀知道在哪里把它渲染。但是,由于局部视图没有布局,它不知道该怎么做第的JavaScript,因此不会在任何地方对其进行渲染。

The above scenario doesn't work because the partial view doesn't have a master layout defined. View, on the other hand, does have Layout with RenderSection defined. If I move section JavaScript from Partial View to View, Razor knows where to render it to. But, since partial view doesn't have a layout, it doesn't know what to do with section JavaScript and thus doesn't render it anywhere.

推荐答案

我不相信的局部视图可以设置部分在布局页中使用:(

I don't believe a Partial View can set sections to be used in the layout page :(

它看起来像有这个没有很好的解决方案 - 你可以包括两个谐音(一个用于脚本,一个用于内容):

It looks like there are no nice solutions to this - you could include two partials (one for script, one for content):

<script stuff>
@Html.RenderAction("PartialViewScripts", "PartialController")
</script stuff>
<body stuff>
@Html.RenderAction("PartialView", "PartialController")
</body stuff>

这篇关于如何呈现JavaScript成从局部视图MasterLayout部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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