Orchard CMS 将数据从视图传递到主题布局
我正在编写一个带有自定义主题的自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
比这更容易...所有模板都可以访问布局形状,这是一个动态对象。这意味着您可以即时修改它、添加它的属性等。
在您的特定情况下,抑制区域,您甚至可以将其设置为空:区域只是形状,在顶级区域的情况下,它们是布局上的展开。因此,如果您有一个名为 Foo 的区域,将 Layout.Foo 设置为 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: