ASP.NET MVC - 模型通知视图?

发布于 2024-10-05 02:15:50 字数 108 浏览 0 评论 0原文

在MVC的一些经典描述中,模型通过观察者模式通知视图。在我看来,ASP.NET MVC 不会发生这种情况,因此模型、视图和控制器之间的基本关系之一缺失了。

是这样吗?如果是这样,为什么?

In some of the classic descriptions of MVC, the Model notifies Views via the observer pattern. It seems to me that this doesn't happen with ASP.NET MVC, and so one of the fundamental relationships between Model, View, and Controller is missing.

Is this the case? If so, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-10-12 02:15:50

ASP.NET MVC 中的视图是无状态的。它们存在的时间很短,然后被发送给客户端。

该过程是这样的:

  1. 请求进入控制器。

  2. 控制器检索模型并实例化视图(将模型传递给它)。

  3. 渲染视图,将标记返回到客户端,然后处理掉。

因此,由于视图在发送到客户端后不再存在......没有任何关于模型更改的通知。

Views in ASP.NET MVC are Stateless. They exist for a very short time and then are sent down to the client.

The process goes something like this:

  1. Request comes in to the Controller.

  2. Controller retrieves the Model and instantiates the View (passing it the Model).

  3. The View is rendered, the markup returned to the client, and then disposed of.

Therefore, since the View no longer exists after it is sent to the client...there's nothing to notify about Model changes.

孤君无依 2024-10-12 02:15:50

不需要观察者模式。在 View 的原始定义中是这样说的:

视图附加到其模型(或
模型部分)并获取数据
演示所必需的
通过提问来建立模型。

http://heim.ifi.uio.no/~trygver/ 2007/MVC_Originals.pdf

在网络应用程序的上下文中,唯一可能的视图是浏览器呈现的标记(HTML/XML)。因此,与 ASP.NET MVC 中一样,视图代码被传递给一个模型实例,它可以访问该实例以向用户提供信息。

Observer pattern is not needed. In the original definition of View it says:

A view is attached to its model (or
model part) and gets the data
necessary for the presentation from
the model by asking questions.

http://heim.ifi.uio.no/~trygver/2007/MVC_Originals.pdf

In the context of a web app, the only View possible is markup (HTML/XML) rendered by a browser. So, as in ASP.NET MVC, the View code is handed a Model instance that it can access to provide information to the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文