This works:
[HttpPost]
public ActionResult Slots(Slots slots, ICollection<int> jobNos)
{
if (!ModelState.IsValid)
return View(new SlotsViewModel() { JobNos = jobNos, Slots = slots });
//
// Do stuff
//
return View("MyResults", jobNos);
}
public ActionResult MyResults(ICollection<int> jobs)
{
return View(jobs);
}
However, this does not work.
[HttpPost]
public ActionResult Slots(Slots slots, ICollection<int> jobNos)
{
if (!ModelState.IsValid)
return View(new SlotsViewModel() { JobNos = jobNos, Slots = slots });
//
// Do stuff
//
return RedirectToAction("MyResults", new { jobs = jobNos });
}
public ActionResult MyResults(ICollection<int> jobs)
{
return View(jobs);
}
When I redirect to the new action the ICollection jobs is empty when it should have a count > 0.
Anyone got an idea why redirecting would empty the collection?
RedirectToActionreturns a 302 response to the client browser and thus the browser will make a new GET requst to the specified URL.In this case, If you really want to pass the collection between these two (stateless) HTTP requests, You need some temp storage mechanism like
TempDataAnd Read it in the other ActionMethod
TempDatausesSessionobject behind the scene to store the data. But once the data is read the data is terminated.