asp.net-mvc ASP.NET MVC 模型与 ViewModel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4061440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:33:05  来源:igfitidea点击:

ASP.NET MVC Model vs ViewModel

asp.net-mvcasp.net-mvc-2modelviewmodel

提问by Qcom

OK, I have been hearing discussion about "ViewModels" in regards to MS's ASP.NET MVC.

好的,我一直在听有关 MS 的 ASP.NET MVC 的“ViewModels”的讨论。

Now, that is intended to be a specific kind of Model, correct? Not a specific kind of View.

现在,这是一种特定类型的模型,对吗?不是特定类型的视图。

To my understanding, it's a kind of Model that has a specific purpose of interacting with the View? Or something like that?

据我了解,它是一种具有与视图交互的特定目的的模型?或类似的东西?

Some clarification would be appreciated.

一些澄清将不胜感激。

采纳答案by Lorenzo

Essentially Model and View Model are both simple classes with attributes.

本质上模型和视图模型都是具有属性的简单类。

The main objective of these classes are to describe (to "Model") an object for their respective audiences that are respectively the controller and the view.

这些类的主要目标是为它们各自的受众(分别是控制器和视图)描述(“建模”)一个对象。

So you are completely right when you say

所以你说的完全正确

To my understanding, it's a kind of Model that has a specific purpose of interacting with the View

据我了解,它是一种具有与视图交互的特定目的的模型

So, while Model classes are effectively Domain Entities that your application interact with, View Models are simple classes that your views interact with.

因此,虽然模型类是您的应用程序与之交互的有效领域实体,但视图模型是您的视图与之交互的简单类。

Hope it helps :)

希望能帮助到你 :)

Update:

更新

Microsoft has developed a specialized version of the Presentation Pattern by Martin fowler largely based on the Model-View-Controller and called it Model-View-ViewModel (MVVM) for PF application. This pattern is targeted at modern UI development platforms where UI developers have different requirements based more on business logic than traditional developers. Have a look herefor a bit of theory

微软开发了一个由 Martin fowler 编写的演示模式的专用版本,主要基于模型-视图-控制器,并将其称为模型-视图-视图模型 (MVVM),用于 PF 应用程序。此模式针对现代 UI 开发平台,在这些平台上,UI 开发人员与传统开发人员相比,更多基于业务逻辑的需求有所不同。看看这里的一些理论

回答by Jason Marsell

In the simplest of terms, I like to think of the following:

用最简单的术语来说,我喜欢考虑以下几点:

Model:Strictly looks and feels like your data model. For all intents and purposes it is only a class representation of your data model. It has no knowledge of your View or any elements within your View. That said, it should not contain any attribute decorators (ie; Required, Length, etc.) that you would use for your View.

模型:严格的外观和感觉就像您的数据模型。出于所有意图和目的,它只是您的数据模型的类表示。它不知道您的 View 或您的 View 中的任何元素。也就是说,它不应包含您将用于视图的任何属性装饰器(即,必需、长度等)。

View Model:Serves as a data-binder between your View and your Model and in many cases, is also a wrapper for your Model. It would be rendered useless without the View, so it typically isn't reusable across multiple Views and Controllers like a standard Model is.

视图模型:作为视图和模型之间的数据绑定器,在许多情况下,也是模型的包装器。如果没有视图,它将变得无用,因此它通常不能像标准模型那样跨多个视图和控制器重用。

As an example, your Model may have the following properties, which are direct representations of your data source:

例如,您的模型可能具有以下属性,它们是您数据源的直接表示:

    public string FirstName { get; set; }
    public string LastName { get; set; }

Now, since your View Model is tied to your View, it may have the following property - which concatenates the Model's FirstName field and LastName field together as one string:

现在,由于您的视图模型与您的视图相关联,它可能具有以下属性 - 将模型的 FirstName 字段和 LastName 字段连接在一起作为一个字符串:

    [Display(Name = "Customer Name")]                
    public string CustomerFullName { get { return String.Format("{0} {1}", myModel.FirstName, myModel.LastName) }}

回答by misteraidan

I found this article a very useful resource for understanding how the "Domain Model" and "View Model" interact within an MVC application, particularly in regards to binding. Best of all includes examples instead of abstract descriptions.

我发现这篇文章是理解“域模型”和“视图模型”如何在 MVC 应用程序中交互的非常有用的资源,尤其是在绑定方面。最重要的是包括示例而不是抽象描述。

"Since MVC has been released I have observed much confusion about how best to construct view models. Sometimes this confusion is not without good reason since there does not seem to be a ton of information out there on best practice recommendations. Additionally, there is not a “one size fits all” solution that acts as the silver bullet. In this post, I'll describe a few of the main patterns that have emerged and the pros/cons of each. It is important to note that many of these patterns have emerged from people solving real-world issues."

“自从 MVC 发布以来,我观察到很多关于如何最好地构建视图模型的困惑。有时这种困惑并非没有充分的理由,因为似乎没有大量关于最佳实践建议的信息。此外,没有一个“一刀切”的解决方案,作为灵丹妙药。在这篇文章中,我将描述一些已经出现的主要模式以及每个模式的优缺点。重要的是要注意这些模式中的许多已经从解决现实世界问题的人们中脱颖而出。”

http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

回答by Ian Mercer

WikiPedia has a more complete description of Model vs. ModelView than you'll get in an SO answer: http://en.wikipedia.org/wiki/Model_View_ViewModel

维基百科对 Model vs. ModelView 的描述比你在 SO 答案中得到的描述更完整:http: //en.wikipedia.org/wiki/Model_View_ViewModel

I quote:

我引用:

Model: as in the classic MVC pattern, the model refers to either (a) an object model that represents the real state content (an object-oriented approach), or (b) the data access layer that represents that content (a data-centric approach).

模型:在经典的 MVC 模式中,模型指的是 (a) 表示真实状态内容的对象模型(面向对象的方法),或 (b) 表示该内容的数据访问层(数据-中心方法)。

View: as in the classic MVC pattern, the view refers to all elements displayed by the GUI such as buttons, windows, graphics, and other controls.

视图:在经典的 MVC 模式中,视图是指 GUI 显示的所有元素,如按钮、窗口、图形和其他控件。

ViewModel: the ViewModel is a “Model of the View” meaning it is an abstraction of the View that also serves in data binding between the View and the Model. It could be seen as a specialized aspect of what would be a Controller (in the MVC pattern) that acts as a data binder/converter that changes Model information into View information and passes commands from the View into the Model. The ViewModel exposes public properties, commands, and abstractions. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.

ViewModel:ViewModel 是“视图的模型”,这意味着它是视图的抽象,也用于视图和模型之间的数据绑定。它可以被看作是控制器(在 MVC 模式中)的一个专门方面,它充当数据绑定器/转换器,将模型信息更改为视图信息并将命令从视图传递到模型。ViewModel 公开公共属性、命令和抽象。ViewModel 被比作数据的概念状态,而不是模型中数据的真实状态。

回答by tsimon

There is a notion of a ViewModel, but it is not generally associated with Asp.net MVC. MVC uses the Model View Controller patter, where the controller handles interactions, builds up data from the Model, and then passes that data to the View for display.

有一个 ViewModel 的概念,但它通常与 Asp.net MVC 无关。MVC 使用模型视图控制器模式,其中控制器处理交互,从模型构建数据,然后将该数据传递给视图进行显示。

ViewModels (and the Model View ViewModel pattern) is more generally associated with Silverlight and WPF. Xaml is a bit different in that the views can do two-way binding to the ViewModels, so the technology is a little different. For example, if you bind a textbox to a field, as you type into that textbox, the value of the field is updated dynamically. This sort of interaction isn't really possible in web pages since web pages are stateless.

ViewModels(和 Model View ViewModel 模式)更普遍地与 Silverlight 和 WPF 相关联。Xaml 有点不同,因为视图可以对 ViewModel 进行双向绑定,所以技术有点不同。例如,如果您将一个文本框绑定到一个字段,当您在该文本框中键入内容时,该字段的值会动态更新。由于网页是无状态的,因此这种交互在网页中是不可能的。

The similarity in the two patterns is that they are both trying to separate the logic from the display. The most common use/reason for this is testing: you want to be able to perform from code (via a testing framework) all the interactions that a user will invoke via the User Interface.

这两种模式的相似之处在于它们都试图将逻辑与显示分开。最常见的用途/原因是测试:您希望能够从代码(通过测试框架)执行用户将通过用户界面调用的所有交互。