Orchard CMS 将数据从视图传递到主题布局

发布于 2025-01-03 01:27:11 字数 414 浏览 3 评论 0原文

我正在编写一个带有自定义主题的自定义 Orchard 模块。我想将数据从视图传递回布局,以根据视图中确定的数据更改布局。

在我的示例中,我的主题中有一个左侧导航 - 某些视图应该能够指示布局不要渲染此左侧导航。

在标准 mvc 3 中,我只会通过 ViewBag 向上传递值,但这似乎在 Orchard 中不起作用 - 我猜测布局代码是在渲染视图之前执行的?

我已经研究过使用形状来移动这些数据,但看起来对于我想要实现的目标来说它可能有点重量级。

像这样传递数据作为 ViewBag 的替代方案的最佳实践是什么?

更新:再玩一会儿后,我刚刚注意到 TempData 正在布局中被拾取 - 谁能解释为什么 TempData 被传输,但 ViewData 没有?从 Orchard POV 使用安全吗?

I'm writing a custom Orchard module with a custom Theme. I would like to pass data from the View back to the Layout to change the layout based on data determined in the View.

In my example, I have a left-nav in my theme - certain views should be able to instruct the layout not to render this left-nav.

In standard mvc 3 I would just pass the value up through the ViewBag, but this doesn't seem to be working within Orchard - I'm guessing that the layout code is executed before the View is rendered?

I've looked into using a Shape to move this data around but looks like it might be a little heavyweight for what i'm trying to achieve.

What's the best practice for passing data around like this as an alternative to ViewBag?

Update: After playing around with it a bit more i've just noticed that TempData is being picked up in the Layout - can anyone explain why TempData is transmitted, but ViewData isn't? And is it safe to use from an Orchard POV?

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

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

发布评论

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

评论(1

落花浅忆 2025-01-10 01:27:11

比这更容易...所有模板都可以访问布局形状,这是一个动态对象。这意味着您可以即时修改它、添加它的属性等。
在您的特定情况下,抑制区域,您甚至可以将其设置为空:区域只是形状,在顶级区域的情况下,它们是布局上的展开。因此,如果您有一个名为 Foo 的区域,将 Layout.Foo 设置为 null 应该可以解决问题。事实上,我在我的一个主题中正是这样做的,以抑制错误页面中的侧边栏,而无需创建特定的小部件层:

Layout.AsideFirst = null;
Layout.AsideSecond = null;

It's easier than that... All templates have access to the Layout shape, which is a dynamic object. This means that you can modify it on the fly, add it properties, etc.
In your specific case, suppressing a zone, you may even be able to just set that to null: zones are just shapes, and in the case of top-level zones they are expandos on Layout. So if you have a zone named Foo, setting Layout.Foo to null should do the trick. As a matter of facts, I'm doing exactly that in one of my themes, to suppress the side bars from my error pages without having to create a specific widget layer:

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