MVC3 .net 多模型或单视图模型
我想构建一个有 3 个选项卡(因此有 3 个 div)的网页: 选项卡 1 - 更新个人详细信息 选项卡 2 - 检查订单 选项卡 3 - 更改登录详细信息
据我所知,有两种方法可以实现此目的,但我想知道最佳实践。
首先是有 3 个部分视图,每个视图实现一个单独的模型 第二种是拥有 3 个部分视图来实现单个 ViewModel
第一个选项如果使用部分视图将导致“期望模型 A 但得到模型 B”的错误 我相信您可以使用 RenderAction 来解决这个问题,但这是最佳实践吗?
欢迎提出意见。
谢谢
I want to build a webpage which has 3 tabs (and thus 3 divs):
Tab 1 - Update Personal Details
Tab 2 - Check Order
Tab 3 - Change logon details
There are 2 ways to implement this as far as I can tell, but would like to know best practice.
First would be to have 3 Partial Views that implement a separate Model each
Second would be to have 3 Partial Views that implement a single ViewModel
The first option if using Partial Views will cause an error of "Expect Model A but got Model B"
I believe you can use RenderAction to get around this, but is that best practice?
Opinions welcome.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看选项卡的名称,“更新个人详细信息”、“检查订单”和“更改登录详细信息” - 似乎这些选项相当不同,但用户可以执行的相关选项。
对我来说,将它们分成具有三个不同视图的三个不同操作是有意义的,这意味着您只渲染可见选项卡的操作。这样,如果用户只想更新他们的详细信息,服务器不会执行数据库查询来检查顺序
您将使用一些 css 对其进行样式设置,使其看起来像三个选项卡,但每个选项卡实际上是一个执行新请求的链接,只是让它看起来像有三个选项卡。
例如,
CheckOrder 和UpdateLogonDetails 的操作将呈现本质上相同的视图,但将显示不同的选项卡。
这会造成一种错觉,以为它都是使用选项卡运行的,尽管事实并非如此。
然后,当所有这些都运行时,您可以使用 JQuery 更新这些链接,以通过 AJAX 触发请求,将结果作为部分视图返回,并使用结果更新内容 div。这意味着启用 Javascript 的人可以获得正确的“ajax”体验,但对于禁用 javascript 的人来说它仍然可以正常工作。
Looking at the names of the tabs, 'Update Personal Details', 'Check Order' and 'Change logon details' - it seems that these are fairly different, but related options that a user could do.
To me, it would make sense to split these up into three different actions with three different views, meaning that you only render the action for the visible tab. This way, if the user only wanted to, say, update their details, the server isn't doing up the DB queries to check the order
You would use some css to style it up so that it looks like three tabs, but each tab really would be a link which which did a new request, just making it look like there were three tabs.
e.g.
The actions for CheckOrder and UpdateLogonDetails would render essentially the same view, but a different tab would be showing.
This would create the illusion that it was all run using tabs, even though it wasn't.
Then, when you had that all running, you could update those links using JQuery to fire the requests via AJAX, returning the results as Partial Views and updating the content div with the result. This means that people with Javascript enabled get the proper 'ajax' experience, but that it still works OK for those with javascript disabled.
user1079925,
正如您所说,有多种方法可以做到这一点,这完全取决于数据的“相关性”程度。如果它们全部源自同一个控制器,那么使用单个视图模型来封装数据是有意义的。这是一个关于它的外观的快速潦草的说明:
这在您的视图中将被引用为:
现在,这并不漂亮(我将把这部分留给您),但允许您从单一视图模型。正如您提到的,另一种选择是如果数据在任何方面完全不相关,则在选项卡 div 内有 3 个 @RenderAction() 块。
user1079925,
As you say, there are a variety of ways to do this and it will all depend on how 'related' the data is. if it all derives from the same controller, then it would make sense to have a single viewmodel to encapsulate the data. Here's a quick scribble as to how that might look:
this would be referenced in your view as:
now, this isn't pretty (i'll leave that part for you), but would allow you to populate your 'tabs' from a single viewmodel. the other alternative, as you mention, is to have 3
@RenderAction()
blocks inside the tab divs if the data is totally unrelated in any way.