views:

706

answers:

2

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?

+3  A: 

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.

Andrew Siemer
A: 

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.

scottrakes