我有 read online使用“厨房水槽”模型是不好的做法:
Rule #3 – The View dictates the design of the ViewModel. Only what is required to render a View is passed in with the ViewModel.
If a Customer object has fifty properties, but one component only shows their name, then we create a custom ViewModel type with only those two properties.
然而,Jimmy Bogard 随后对这有多好的解释让我有点疑问。让我的模型只包含客户列表非常容易,我什至可以使用我的 POCO。
那么现在我可以为网站上的每个页面创建自定义的小 View 模型片段了吗?使用 Customer 属性的每个页面都会得到一个,但当然不能共享,因为某些信息是无关紧要的,例如,如果一个页面使用 Age 而不是 Name。两个新的迷你 View 模型类对吗?
这是非常耗时的,并且似乎会导致一百万个小的自定义 View 模型 - 有人可以详细说明这种方法的实用性以及为什么更简单的方法不好?
最佳答案
View 模型类不仅可以用于传输值,还可以定义与模型中使用的不同的数据类型(数据注释)、验证规则和关系。我现在想到的一些优点:
更改他的基本数据或订阅设置。有可能
在一个模型类中定义所有这些规则很复杂。它看起来
使用不同的 View 模型时会更好更干净。
要显示用户列表,您可以使用 id 和 name 定义 View 模型
only 并使用索引从数据库中检索它。如果你检索到
整个对象并将其传递给 View ,您可以从中传输更多数据
比您需要的数据库。
关于asp.net-mvc - asp.net MVC 3 中 View 模型的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317425/