基于上下文动态组合 ASP MVC 视图
我有几个视图,它们由基于上下文的多个部分视图组成。
例如,我们有一个项目视图,显示项目的所有详细信息,其中包括标量值、名称等,以及所有分配的员工、任务和/或客户。
我遇到的问题是,某些类型的项目具有上述所有部分,而其他项目只有两个甚至一个部分,即。仅详细信息。
构建项目主视图的最佳方式是什么?我不想有逻辑来检查视图中的项目。有没有一种方法可以通过以编程方式渲染相关部分并忽略其余部分来在代码中组成视图?
否则还有其他想法如何以可维护的方式做到这一点吗?我当然可以使用 if 语句渲染部分内容来检查它们是否适用,但这样视图就包含非常重要的逻辑。在另一种情况下,我们希望使用此方法根据用户的订阅类型显示内容。
谢谢!
I have several views that are composed of several partial views based on context.
For example, we have a Project view that shows all the details of a project, which includes the scalar values, name etc, and also all the assigned employees, tasks and/or clients.
The problem I have is that certain types of project have all of the above sections while others have only two or even on section, ie. details only.
What is the best way to compose the Projects master view? I don't want to have logic to check the project in the view. Is there a way to compose a view in code by programatically rendering the relevant partials and ignoring the rest?
Otherwise are there any other ideas how to do this in a maintainable way? I could of course just render the partials using if statements to check if they apply, but that way the view contains VERY important logic. In another situation we want to use this method to display content based on the type of subscription a user has.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会坚持使用
if
方法。为了避免在视图中编写重要逻辑,请在视图模型中定义属性并让控制器设置值,以便在视图内您只有:或者如果您正在使用显示/编辑器模板,您可以简单地:
I would stick with the
if
approach. To avoid coding important logic in the view define properties in your view model and let the controller set the value so that inside the view you only have:Or if you are working with display/editor templates you could simply: