0

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.

gordana
  • 23
  • 6
  • Maybe must set [Cookie.IsEssential to true](https://stackoverflow.com/a/54813987) – Dialecticus Nov 29 '19 at 10:57
  • I may be wrong but that looks like you're going from A (Experiment) to B(?) to C (Experiment1[Post]) in terms of redirects. TempData will go from A to B then in order to get it to C you would need to process it to a variable (in the View) then post it to C with your other data. – scgough Nov 29 '19 at 11:00
  • From Darko: `This may be helpful resource [When to use ViewBag, ViewData, or TempData in ASP.NET](https://rachelappel.com/2014/01/02/when-to-use-viewbag-viewdata-or-tempdata-in-asp-net-mvc-3-applications/)` – Panagiotis Kanavos Nov 29 '19 at 11:13
  • @scgough the variable nextExperiment is a random string, either Experiment1, Experiment2 or Experiment3. In this example I copied the code from ActionResult Experiment1 just for the example. Thank you for your reply! – gordana Nov 29 '19 at 11:22
  • @PanagiotisKanavos I don't need to retrieve the data in the views (which I understand ViewBag and ViewData is for. I only wanna pass the data from one ActionResult to another within the same Controller. Thank you for the reply! – gordana Nov 29 '19 at 11:23

1 Answers1

1

You must first post the TempData["List"] ,because that reset after postback :

<input type="hidden" name="mylist" value="@TempData["List"]">



 [HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Experiment1(ExperimentViewmodel experiment,mylist[])
{
    var newExperiment = new Experiment
    {
        Question1 = experiment.SelectededItem
    };

    if (ModelState.IsValid)
    {
        _context.Update(newExperiment);
        await _context.SaveChangesAsync();

        var stringList = mylist as List<string>; 
        var nextExperiment = stringList.FirstOrDefault(); 
        stringList.RemoveAt(0);                           
        TempData["List"] = stringList;                  


        return RedirectToAction(nextExperiment, new { id = newExperiment.EID });
    }
    return View();
}
RezaGhahari
  • 405
  • 3
  • 9