我可以从 Razor 部分视图中填充母版页中的 ContentPlaceHolder 吗?

发布于 2024-12-10 00:12:32 字数 331 浏览 0 评论 0原文

我在 mvc3 中使用典型的内置视图引擎(它有合适的名称吗?)来查看视图和母版页,它包括 .aspx 页面上的 Razor 部分视图。在母版页中,有一个 ID 为“ScriptContent”的 ContentPlaceHolder

我希望能够从 Razor 部分视图中填充 ContentPlaceHolder 但我认为这是不可能的。有谁知道这是否可能以及我将如何去做?

我已经尝试像这样部分渲染它,但这不起作用。

@section ScriptContent {
    ... content ...
}

I'm using the typical built in view engine in mvc3 (is there a proper name for it?) for views and master pages and it's including a Razor partial view on the .aspx page. In the masterpage, there is a ContentPlaceHolder with an ID of "ScriptContent".

I want to be able to fill that ContentPlaceHolder from within the Razor partial view but I don't think this is possible. Does anyone know if it is possible and how I would go about doing that?

I already tried rendering it in the partial like so, but that didn't work.

@section ScriptContent {
    ... content ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-12-17 00:12:32

这将非常困难,以至于我建议寻找另一种方法:(。我希望它更容易,但这些是将新的视图引擎集成到现有遗留系统中的复杂性。

如果你真的想尝试一下:你可能需要为你的 Razor 内容页面创建一个继承自 WebViewPage 的自定义基类,重写一些方法(老实说,我对这方面不太熟悉,所以你需要调试遵循管道),这样如果将 Layout 属性视为布局页面的路径,则将其视为母版页,然后需要实例化母版页并以某种方式转换部分(这些部分被 Razor 解析器转换为对 DefineSection 的调用,并且应该存储在基类上的字典中)到内容控件中并将它们填充到母版页中,

如果我到目前为止还没有让您感到困惑,那么您可能能够做到这一点,但说实话。 ,我会避免 。

PS 根据其文件扩展名,我们将旧的视图引擎称为“ASPX”;)

It would be very difficult, so much so that I'd recommend finding another way :(. I wish it was easier, but these are the complexities of integrating a new view engine into an existing legacy system.

To give you a head start if you really want to try it: You'd probably need to create a custom base class inheriting from WebViewPage for your Razor content pages, override some of the methods (honestly I'm not too familiar with that aspect so you'd need to debug to follow the pipeline) so that instead of treating the Layout property as the path to a Layout page, you treat it as a Master page. Then you'd need to instantiate the master page and somehow convert the Sections (which were transformed into calls to DefineSection by the Razor parser, and should be stored in a Dictionary somewhere on the base class) in to Content controls and stuff them in the Master Page.

If I haven't boggled your mind by this point, you may just be able to pull this off, but to be honest, I'd avoid it.

P.S. We refer to the older view engine as "ASPX", based on its file extension ;).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文