Reputation: 231
I have been searching high/low on the GooglePlex and here for a sample MVC3 Wizard (multi-step) that does NOT invlove the clientsidevalidation elements of MVC3 (and jQuery). I have seen at least one detailed explanation of some elements here: multi-step registration process issues in asp.net mvc (splitted viewmodels, single model), but I have not been able to get it to work properly.
Being new to MVC3 and programming in general, I suspect its my experience level that's preventing me from getting that to work.
Anyhow, any guidance to non-Jquery MVC3 wizard samples would be great. Thank you in advance.
Upvotes: 1
Views: 6457
Reputation: 448
Jonas is absolutely right. A little further breakdown below.
public class MyModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set };
public string StepOneData { get; set; }
public string StepTwoData { get; set; }
}
The above coed is stupid simple so replace your fields in there. Next we start with a simple action that initiates our wizard.
public ActionResult WizardStep1()
{
return View(new MyModel());
}
This calls the view "WizardStep1.cshtml (if using razor that is). You can use the create template wizard if you want. We will just be redirecting the post to a different action.
<WizardStep1.cshtml>
@using (Html.BeginForm("WizardStep2", "MyWizard")) {
The thing of note is that we will be posting this to a different action; the WizardStep2 action
[HttpPost]
public ActionResult WizardStep2(MyModel myModel)
{
return ModelState.IsValid ? View(myModel) : View("WizardStep1", myModel);
}
In this action we check if our model is valid, and if so we send it to our WizardStep2.cshtml view else we send it back to step one with the validation errors. In each step we send it to the next step, validate that step and move on. Now some savvy developers might say well we can't move between steps such as this if we use [Required] attributes or other data annotations between steps. And you would be right, so remove the errors on items that are yet to be checked. like below.
[HttpPost]
public ActionResult WizardStep3(MyModel myModel)
{
foreach (var error in ModelState["StepTwoData"].Errors)
{
ModelState["StepTwoData"].Errors.Remove(error);
}
Finally we would save the model once to the data store. This also prevents a user that starts a wizard but doesn't finish it not to save incomplete data to the database.
I hope you find this method of implementing a wizard much easier to use and maintain than any of the previously mentioned methods.
Thanks for reading.
Upvotes: 3
Reputation: 101192
I would do something like this if jQuery/javascript is not allowed to be used
Upvotes: 4