gpt4 book ai didi

asp.net-mvc - mvc中禁用或不禁用按钮的逻辑

转载 作者:行者123 更新时间:2023-12-02 02:33:53 25 4
gpt4 key购买 nike

这是我正在阅读的一本关于使用 MVC 进行应用程序设计的书的摘录:

Ideally, the view is so simple and logic-free as to need virtually no testing. Users (and developers before users) can reasonably test the view by simply looking at the pixels on the screen. Anything else beyond pure graphical rendering should ideally be taken out of the view and placed in the controller and model. This includes, for example, the logic that determines whether a certain button should be enabled or grayed out at some point.

大胆的声明对您意味着什么?这会是什么样子?

谢谢,杆。

最佳答案

决定何时启用或禁用按钮的逻辑应该驻留在 Controller 中,并简单地调用一个方法,例如 view.EnableContinueButton() 来启用/禁用页面上的按钮。

启用/禁用页面上按钮的实际代码应该在 View 中实现,例如 EnableContinueButton() 方法,然后调用类似 btnContinue.Enable() .

简单地说, View 应该关注 UI 细节(显示/隐藏/启用/禁用 UI 元素)并将所有业务逻辑处理留给 Controller 。通过这种方式, Controller 无需关心 UI 元素, View 独立于实际业务逻辑工作。

例如在 Controller 中,

public void ProcessOrder()
{
if (!controller.ValidateOrder(model.OrderNo))
view.EnableContinueButton(false);
else
// Process the order
...
}

在 View 中

public void EnableContinueButton(bool enabled)
{
btnContinueButton.Enabled = enabled;
}

坦率地说,我在 MVC 方面没有太多经验(前一段时间在一个项目中实现过),但我希望 Controller 和 View 之间的逻辑分离足够清晰。

关于asp.net-mvc - mvc中禁用或不禁用按钮的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2650436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com