ASP.NET MVC 2 RC 2“向导”用于整个模型验证
对于 MVC 中的多页面“向导”控件,这里有一个很好的简单解决方案:
http:// /www.highoncoding.com/Articles/647_Creating_Wizard_Using_ASP_NET_MVC_Part_1.aspx
该模型通过几个步骤填充,并使用隐藏字段在页面之间保留数据(有点类似于 ViewState)。然而,随着 MVC 2 RC2 的发布,验证机制已从“输入验证”更改为“模型验证”: http:// bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html
现在“向导”中的第一页永远不会得到验证,因为它只填充模型的一部分。 (其余部分将在后续步骤中填充,但由于可能存在必填字段,因此在验证第一页期间会显示验证错误,并且用户无法继续)。
有没有办法修改“向导控制”的实现以适应 MVC RC2,或者应该重新考虑整个逻辑?是否有更好的模式来创建多页面“向导控件”来填充模型?
There is a good simple solution for a multi-page "Wizard" control in MVC here:
http://www.highoncoding.com/Articles/647_Creating_Wizard_Using_ASP_NET_MVC_Part_1.aspx
http://www.highoncoding.com/Articles/652_Creating_Wizard_in_ASP_NET_MVC_Part_2.aspx
The model is populated in several steps and a hidden field is used to persist data between pages (somewhat similar to ViewState). However, with the release of MVC 2 RC2, the validation mechanism has been changed from "input validation" to "model validation":
http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html
Now the first page in the "Wizard" never gets validated, since it only populates part of the model. (The rest is to be populated during further steps, but since there can be required fields, validation errors are shown during the validation for the first page and the user can't proceed).
Is there a way to modify this implementation of the "Wizard control" to suit MVC RC2, or should the whole logic be re-thought? Are there any better patterns for creating a multi-page "wizard control" for populating a model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我简要阅读了列出的文章。我认为问题在于您将域对象(演示中的客户)传递到两个屏幕进行编辑。一半的客户在第一个屏幕中进行编辑,另一半在下一个屏幕中进行编辑。
在这里应该对您有所帮助的模式是,您应该(恕我直言)只传递“视图模型”,而不是将实际模型(客户)传递到演示文稿。这就是说,每个视图(或本例中向导中的步骤)都有自己的模型。 CustomerNameViewModel 和 CustomerAddressViewModel 可能是列出的教程的合适对象。这意味着每个对象在返回进行验证时都将完全填充数据。一旦对象被适当验证,您就可以填充您的客户对象。一旦客户对象在向导结束时完成,您就可以保留该客户对象。
I read through the listed articles briefly. I think the problem is that you are passing a domain object (customer in the demo) to both screens for edit. Half of the customer gets edited in the first screen and the other half is edited on the next screen.
The pattern that should help you here is that rather than passing your actual model (customer) to your presentation you should (IMHO) only be passing out a "view model". This is to say that each view (or step in the wizard in this case) would have its own model. CustomerNameViewModel and CustomerAddressViewModel might be appropriate objects for the listed tutorial. This would mean that each object would be populated with data fully when they come back for validation. Once the object has been validated appropriately you could fill your customer object. Once the customer object is completed at the end of the wizard you would then persist the customer object.
查看 http://blog.stevensanderson。 com/2010/02/19/partial-validation-in-aspnet-mvc-2/。 Steven 展示了如何使用 ActionFilter 进行部分视图验证。
Checkout http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/. Steven shows how to use an ActionFilter to do partial view validation.