I cannot get the logic for this down correctly. I am using a wizard with separate views in the controller (no javascript). Based on a selection in Step 2 of the wizard I want to either skip Step 7 or show Step 7. The wizard uses next/back buttons to control where to go.
I was able to “show” Step 7 if the user selected “A” (for example), but when I created the logic to skip Step 7 if either “B” or “C” were selected I either get a “redirect loop / too many redirects error” on Chrome (I cleared out the cookies to no avail) or the “Next” button on the prior step won’t work (it just shows the same view).
Step 2 in and of itself is unimportant in terms of the controller, it contains a drop down list with the 3 choices, and based on that choice this is the controller code I have (I leave out serializing the myViewModel code in the Controller, which is decorated with [Serializable]):
// STEP 6:
// Based on selection in Step 2, show or don't show Step 7
public ActionResult Step6(string backButton, string nextButton)
{
if (backButton != null)
return RedirectToAction("Step5");
else if ((nextButton != null) &&
ModelState.IsValid &&
(myVieModel.MyModel.MyDropDown ==
MyNamespace.Models.MyModel.MyEnum.A))
return RedirectToAction("Step7");
else if ((nextButton != null) &&
ModelState.IsValid &&
(myVieModel.MyModel.MyDropDown ==
MyNamespace.Models.MyModel.MyEnum.B) ||
(myVieModel.MyModel.MyDropDown ==
MyNamespace.Models.MyModel.MyEnum.C))
return RedirectToAction("Step8");
else
return View("Step6", myViewModel);
}
// STEP 7:
// Only shown if Choice in MyDropDown is "A",
// otherwise if "B" or "C" skipped
public ActionResult Step7(string backButton, string nextButton)
{
if (backButton != null)
return RedirectToAction("Step6");
else if ((nextButton != null) &&
ModelState.IsValid)
return RedirectToAction("Step8");
else
return View("Step7", myViewModel);
}
// STEP 8:
// Arrive here either from Step 6
// (if "B" or "C" chosen),
// or from Step 7 (if "A" chosen)
public ActionResult Step8(string backButton, string nextButton)
{
if ((backButton != null) &&
(myVieModel.MyModel.MyDropDown ==
MyNamespace.Models.MyModel.MyEnum.A))
return RedirectToAction("Step7");
else if ((backButton != null) &&
(myVieModel.MyModel.MyDropDown ==
MyNamespace.Models.MyModel.MyEnum.B) ||
(myVieModel.MyModel.MyDropDown ==
MyNamespace.Models.MyModel.MyEnum.C))
return RedirectToAction("Step6");
else if ((nextButton != null) &&
ModelState.IsValid)
return RedirectToAction("Step9");
else
return View("Step8", myViewModel);
}
I know I am just getting the logic down wrong. Any help is much appreciated.
The above code (without the logic I am asking about) works perfectly, as does the code I use to display the DropDown.
Ugh, I should have encapsulated the
||logic in its own()to read thusly: