Reputation: 18660
I'm developing this really important squirrel application.
There is a wizard where squirrels are added to the database.
So say there are three screens to this wizard:
So at each step of the wizard I'm wanting to save the details to the database.
The Height and weight view looks like:
@model HeightWeightViewModel
@{
ViewBag.Title = "Height and weight";
}
<h2>Height and weight</h2>
@using (Html.BeginForm()) {
<h3>Height</h3>
<div>
@Html.EditorFor(model => model.Squirrel.Height)
</div>
<h3>Weight</h3>
<div>
@Html.EditorFor(model => model.Squirrel.Weight)
</div>
<input type="submit" value="Previous" />
<input type="submit" value="Next" />
}
So I'm hoping that Previous and Next buttons will save these details. The Previous button while saving will also take the user to the Squirrel name details page. The Next will save and take the user to the nut storage page.
I got the Next button working using:
public ActionResult Edit(SquirrelViewModel squirrelViewModel)
{
_unitOfWork.SaveHeightWeight(squirrelViewModel);
return RedirectToAction("Edit", "NutStorage", new { id = squirrelViewModel.Squirrel.Id });
}
So the Next button saves the details and sends the user to the NutStorage page.
The Previous button does the same as Next but I actually want it to send the user to the first step of the Wizard after saving.
I'm not sure how to do this. Would I have another method to post to for Previous?
I can't image how to implement this.
Maybe I should be using ActionLinks instead of submit buttons but that would not post the details to be saved.
Can anyone suggest how to get the previous button to save and send the user to the first page of the wizard while still having the Next functionality working?
Upvotes: 3
Views: 3434
Reputation: 8098
You could set the name of the submit buttons:
<input type="submit" name="direction" value="Previous" />
<input type="submit" name="direction" value="Next" />
Then add that name as a parameter for the controller action and test the value of it to determine what to do:
public ActionResult Edit(SquirrelViewModel squirrelViewModel, string direction)
{
_unitOfWork.SaveHeightWeight(squirrelViewModel);
if (direction == "Next")
return RedirectToAction("Edit", "NutStorage", new { id = squirrelViewModel.Squirrel.Id });
if (direction == "Previous")
return RedirectToAction("Edit", "SquirrelName", new { id = squirrelViewModel.Squirrel.Id });
return View();
}
Upvotes: 10