I need some help!
I'm trying to pass a List from one ActionResult to another using TempData. I'm doing it because I'm trying to make an experiment with three questions, in which the order of each view is randomized from start.
public ActionResult Experiment()
{
List<string> stringArray = new List<string>();
stringArray.Add("Experiment" + rn1.ToString());
stringArray.Add("Experiment" + rn2.ToString());
stringArray.Add("Experiment" + rn3.ToString());
var nextExperiment = stringArray.FirstOrDefault();
stringArray.RemoveAt(0);
var experiment = new Experiment();
_context.Add(experiment);
_context.SaveChanges();
TempData["List"] = stringArray;
return RedirectToAction(nextExperiment, new { id = experiment.EID });
}
But when I'm trying to read the data from stringList it is null.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Experiment1(ExperimentViewmodel experiment)
{
var newExperiment = new Experiment
{
Question1 = experiment.SelectededItem
};
if (ModelState.IsValid)
{
_context.Update(newExperiment);
await _context.SaveChangesAsync();
var stringList = TempData["List"] as List<string>; //stringList = null
var nextExperiment = stringList.FirstOrDefault(); //The next view in the sequence
stringList.RemoveAt(0); //Removing the view from the list since it's already been used.
TempData["List"] = stringList; //Updating TempData with the new list for the next view.
return RedirectToAction(nextExperiment, new { id = newExperiment.EID });
}
return View();
}
Update: The TempData["List"] do contain the values, but I can't figure out how to retrieve the data. When I try to cast it as a List (which would be optimal) var stringList is null.