I have two screens in my application. On the first one, the user selects two values that are passed in to the next controller using the TempData
object (we are not able to use the Session
object).
Then in the controller for my second page, it reads these values and assigns them to a new model. This all works perfectly unless the user refreshes the page. In this case, the TempData
variables are lost, and my model no longer contains the values.
The first controller action (when you select the second item from a select list)
[HttpPost]
public ActionResult Select(ProductSelector model)
{
if (!ModelState.IsValid)
{
ProductSelector newModel = InitialiseProductSelectorModel();
newModel.ProductId = model.ProductId;
newModel.StatusId = model.StatusId;
return View("ProductSelector", newModel);
}
TempData["StatusId"] = model.StatusId;
TempData["ProductId"] = model.ProductId;
return RedirectToAction("Create", "ProductDetails");
}
My second controller action looks like this:
[HttpGet]
public ActionResult Create()
{
int ProductId, StatusId;
// Get ProductId and StatusId from product selector screen
int.TryParse(TempData["ProductId"].ToString(), out ProductId);
int.TryParse(TempData["StatusId"].ToString(), out StatusId);
model.ProductId = ProductId;
model.StatusId= StatusId;
return View("OrderCreate", model);
}
How can I get around this? I have another action which takes the model as a parameter, but this is for the POST
method.