在MVC中,什么是视图模型? [英] In MVC, what is a ViewModel?

查看:316
本文介绍了在MVC中,什么是视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正确的思维,它几乎像一个视图?

Am I right in thinking it's almost like a wrapper for all the objects necessary for a View?

举例来说,假设你有一个在线商店的销量音乐和DVD。在您浏览网页你想显示你所有的DVD和音乐列表。你会因此构建具有视图模型对象,包含专辑列表和DVD的名单?两个属性。

For example, say you had an online store that sold music and dvds. On your browse page you'd want to display a list of all your dvds and music. Would you therefore construct a ViewModel object that has two properties containing an albums list and a dvds list?

从我的理解看来,你有你的所有模型类IE浏览器。一个专辑/ DVD类,而只是单纯路过这些是不够的视图。难道一个视图模型基本上充当所有数据的视图需要一个载体?

From my understanding it seems that you have all your model classes ie. an Album/Dvd class, but simply passing these alone wouldn't be enough for your View. Does a ViewModel basically act as a carrier for all the data your View requires?

推荐答案

你的理解是正确的大部分,但它不是完整的。

Your understanding is mostly correct, but it's not complete.

该视图模型也可以执行从你的模型进行到View可以方便地与工作的数据类型的数据类型转换;这甚至可能意味着视图模型不直接携带模式但携带(可能的子集)在一个更合适的格式相同的信息的其他船只

The ViewModel may also perform conversions from the type of data that your Model carries to the type of data your View can conveniently work with; this might even mean that the ViewModel does not carry Models directly but other vessels that carry (possibly a subset of) the same information in a more suitable format.

考虑,你可以有一个聚集专辑和DVD的图书馆模式 - 这种模式之间的差异,相应的视图模型为precisely该型号不关心(甚至知道),而查看视图模型有利于它的前preSS目的。

Consider that you could have a Library model that aggregates Albums and DVDs -- the difference between such a model and the corresponding ViewModel is precisely that the Model doesn't care (or even know about) the View while the ViewModel has the express purpose of facilitating it.

这篇关于在MVC中,什么是视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆