• I have one view model called ExampleViewModel.
  • I have one controller called ExampleController
  • I have two views called Page1 and Page2.

Both views are strongly typed with ExampleViewModel.

ExampleViewModel contains two properties, Name and Age.

  • Page1 captures data for the property Name.
  • Page2 captures data for the property Age.

When I visit page1, I fill in the textbox for the Name property and hit a 'Next' button which will post the data to the controller.

When debugging you can see the data is posted OK. The data sent back to the post function is a ExampleViewModel object. Then I pass that same object to into Page2.

When I then visit page2, I fill in the textbox for the Age property and hit a Back button which will post the data to the controller.

The problem here is when you look into the ExampleViewModel object that was posted back, the Name property value which was set in Page1 is now lost, and only Page2 data is submitted (Age property).

How do I keep property values of the same object that are passed between views?

ExampleViewModel

public class ExampleViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

ExampleController

public class ExampleController : Controller
{
    [HttpPost]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult PostMethod (ExampleViewModel formData, FormCollection form, string command)
    {
        string newView = "";
        string newController = "";

        Session["formData"] = formData;

        if (command.ToLower() == "back")
        {
            newView = form["PreviousAction"];
            newController = form["PreviousController"];
        }
        else if (command.ToLower() == "next")
        {
            newView = form["NextAction"];
            newController = form["NextController"];
        }

        return RedirectToAction(newView, newController);
    }

    public ActionResult Page1()
    {
        return GetView();
    }

    public ActionResult Page2()
    {
        return GetView();
    }

    private ViewResult GetView()
    {
        ExampleViewModel formData = (ExampleViewModel)Session["formData"];

        if (formData != null)
        {
            return View(formData);
        }
        else
        {
            return View();
        }
    }
}

Page 1 View

@model ExampleProject.ViewModels.ExampleViewModel

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post))
{
   @Html.Label("Your name")
   @Html.TextBoxFor(m => m.Name)

   <input id="ButtonNext" type="submit" name="Command" value="Next" />

   @Html.Hidden("nextaction", "page2")
   @Html.Hidden("nextcontroller", "example")
}   

Page 2 View

@model ExampleProject.ViewModels.ExampleViewModel

@using (Html.BeginForm("PostMethod", "Example", FormMethod.Post))
{
   @Html.Label("Age")
   @Html.TextBoxFor(m => m.Age)

   <input id="ButtonBack" type="submit" name="Command" value="Back" />

   @Html.Hidden("previousaction", "page1")
   @Html.Hidden("previouscontroller", "example")
}

Related posts

Recent Viewed