I have a very simple example I am trying to create. On a button click, I am trying to increment a number in the view model, that's it. The entirety of the code is here:
View Model
public class CustomerOverallViewModel
{
public int currentState { get; set; }
}
Controller
public class WorkflowController : Controller
{
public ActionResult Index()
{
CustomerOverallViewModel viewModel = new CustomerOverallViewModel();
viewModel.currentState = 0;
return View(viewModel);
}
[HttpPost]
public ActionResult Index(CustomerOverallViewModel viewModel)
{
viewModel.currentState++;
return View(viewModel);
}
}
View
@model AgentWebsite.ViewModels.CustomerOverallViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<div>
called from the WorkflowController -> Index method
@Html.HiddenFor(m => m.currentState)
<input type="submit" class="btn btn-success" value="Back" />
<input type="submit" class="btn btn-success" value="Next" />
</div>
}
When I click the buttons, currentState
is always being reset, no matter what. What am I missing? How can I use variables that are not on the page?