GUI 和数据验证 [MVC]
如果视图包含无效数据,谁负责向控制器报告存在无效数据? [坚持之前]。
例如:
Model:
Name [must not be empty, and at least X characters]
Age [must be an integer, greater than 0]
View:
Text box for the name [with some kind of indicator of being invalid]
Text box for the age [with some kind of indicator of being invalid]
当通知控制器数据输入完成时。 【从视图】哪个类负责让控制器知道数据无效? [假设用户没有输入姓名,并输入了非数字的年龄]我可以认为这是模型的责任,因为它直接对数据和上下文负责。不过,我也可以看到视图的责任如何,因为它首先允许输入无效数据。
If a view contains invalid data, who is responsible for reporting to the controller that there exist invalid data? [Before persisting].
For example:
Model:
Name [must not be empty, and at least X characters]
Age [must be an integer, greater than 0]
View:
Text box for the name [with some kind of indicator of being invalid]
Text box for the age [with some kind of indicator of being invalid]
When the controller is notified that the data entry is complete. [From the view] Which class is responsible for letting the controller know that the data is invalid? [Lets say that the user did not enter a name, and put in a non number for the age] I can see it being the model's responsibility because it is directly responsible for the data, and the context. However I can also see how it is the view's responsibility because it allows for the input of the invalid data in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器负责MVC三元组中的“逻辑”,决定数据是否有效肯定是“逻辑”,所以实际上应该由控制器负责决定数据是否有效。也就是说,模型在某种程度上参与此决策并不罕见(例如:通过其成员的验证属性或验证帮助器接口的实现)。
视图根本不应该参与数据是否有效的决策,因为视图不应该包含任何重要的逻辑。视图可能会显示有关模型和/或其部分的有效性的信息(例如:文本框旁边的“坏值”指示器),但显示此信息的决定最终应由控制器做出。
The controller is responsible for the "logic" in an MVC triad, and deciding whether data is valid is definitely "logic", so it's actually the controller that should be responsible for deciding whether the data is valid. That said, it's not uncommon for the model to participate somewhat in this decision (e.g.: via validation attributes on its members or implementation of a validation helper interface).
The view should not participate at all in the decision of whether the data is valid since the view should not include any significant logic. The view might display information concerning validity of the model and/or its parts (e.g.: a "bad value" indicator next to a text box), but the decision to display this information should ultimately be made by the controller.
我想说视图应该询问模型输入是否有效。最终,是模型控制了数据。
I would say the view should ask the model whether the input is valid. In the end, it is the model that controls the data.