ASP.NET MVC术语绊倒我 - 为什么“视图模型”? [英] ASP.NET MVC terminology is tripping me up - why 'ViewModel'?

查看:90
本文介绍了ASP.NET MVC术语绊倒我 - 为什么“视图模型”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个ASP.NET MVC的新手,但pviously使用了许多模型 - 视图 - 控制器框架$ P $。

I'm an ASP.NET MVC newbie, but have used many Model-View-Controller frameworks previously.

<我一个href=\"http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx\">recently碰到收拾你的特定视图需要数据块的约定(事实上,它分配到的的ViewData 的)到一个名为(NameOfView)的视图模型

I recently came across the convention of gathering up the pieces of data that your particular view needs (indeed, it's assigned to the ViewData) into a new class called (NameOfView)ViewModel.

收集了这个数据,以便它由视图/控制器的互动提供的功能让我觉得作为辅助结构,甚至倒闭机制(在'封装的变量的集合的意义上)有关。

Gathering up this data so that it's associated with the functions provided by the View/Controller interaction strikes me as a helper struct, or even closure mechanism (in the 'encapsulates a collection of variables' sense).

那么为什么叫视图模型,因为它既不是查看或型号?

So why is it called 'ViewModel', given that it's neither a View or Model?

没有任何人找到名称混乱?

Does anyone else find the name confusing?

修改:这有什么错只是把性能到视图,以便控制器能够填充它们(如在其他MVC框架)

EDIT: What's wrong with just putting properties onto the View so that the Controller can populate them (as in other MVC frameworks)?

推荐答案

该模型是数据的视图无关的再presentation。该的视图的模型是数据的视图,具体再presentation:它的模式,因为它可能会从给定的视图显示

The model is a view-agnostic representation of the data. The view model is a view-specific representation of the data: it's the model as it might appear from a given viewpoint.

考虑到由原始数据点的模型;然后直方图视图可能包括从数据得出一套水桶和汇总的视图模型。

Consider a model that consists of raw data points; a histogram view might then have a view model consisting of a set of buckets and totals drawn from that data.

从逻辑上讲,它的模型的子集或转化 - 它可以按需以便特定功能和模型作为其唯一的输入来生成

Logically, it's a subset or transformation of the model - it could be generated on-demand with a view-specific function and the model as its only input.

对于在视图与属性包或自定义对象属性...我敢肯定有人有这个强烈的感情,但我个人没有看到很大的区别。你生产模型的视图特定的重presentation,并通过它的不知何故的;确切的机制似乎并不那么重要。

Regarding properties on the view vs. a property bag or custom object... I'm sure someone has strong feelings on this, but personally I don't see the big difference. You're producing a view-specific representation of the model and passing it somehow; the exact mechanism doesn't seem all that important.

这篇关于ASP.NET MVC术语绊倒我 - 为什么“视图模型”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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