模型和视图模型的区别 [英] difference between models and view models
问题描述
我一直在研究一个新项目的 asp.net MVC 项目结构,但有一个问题让我感到困惑.模型和视图模型有什么区别?我说视图模型包含表单属性中的模型是否正确?
I have been researching asp.net MVC project structure's for a new project and have a question about something is confusing me. What is the difference between models and view models? Would I be correct in saying that view models encompass models in the form properties?
推荐答案
我有一个博客,我想在其中显示最新帖子、最新评论的列表, 在单个视图中发布类别.我怎么能做到这一点?我可以强烈地将我的视图输入到任何一个模型中,对吗?出现了视图模型.
I've a blog where I want to display the list of the latest posts, latest comments, post categories in a single view. How I can do that? I can strongly type my view to any one of the model right? There comes view model.
我创建了一个名为 BlogViewModel
的视图模型,其中包含最新帖子、最新评论和其他内容作为属性,并将我的视图与此模型绑定.posts
、comments
.. 是域模型,而 BlogViewModel
是视图模型 我专门为视图创建的.
I created a view model called BlogViewModel
that contains latest posts, latest comments and other stuff as properties and I bind my view with this model. The posts
, comments
.. are domain models while the BlogViewModel
is the view model I created specially for the view.
明天我将在移动版本中展示我的博客,届时我可能会创建一个仅包含较少属性的简单视图模型.最后.. 视图模型用于视图,大多数时候它们充当真实域模型的包装器!
Tomorrow I'll show my blog in a mobile version and at that time I may create a simple view model that contains only less properties. Finally.. view models are for views and most of the times they acts as wrappers over the real domain models!
这篇关于模型和视图模型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!