locked
Sharing Data Between ActionResult Using TempData RRS feed

  • Question

  • User1596943402 posted

    I am trying to share data between these two controllers:

    Doing a foreach loop in the Index View works fine, but when I try to do a foreach loop in View "Two" I get the following error: "System.NullReferenceException: 'Object reference not set to an instance of an object.'"

    		public IActionResult Index()
    		{
    			List<string> lstStudent = new List<string>();
    			lstStudent.Add("Debasis");
    			lstStudent.Add("Samrat");
    			lstStudent.Add("Rahul");
    			TempData["StudentData"] = lstStudent;
    			return View(); 
    		}
    
    		public IActionResult Two()
    		{
    			List<string> modelData = TempData["StudentData"] as List<string>;
    			TempData.Keep();
    			return View(modelData);
    		}



    This is the code for View Two:

    @{
        ViewData["Title"] = "View Two";
        TempData.Keep();
    
    }
    
    <ul>
        @foreach (var data in Model)
        {
            <li>@data</li>
        }
    </ul>
    

    Any assistance would be kindly appreciated.

    Wednesday, August 19, 2020 6:12 PM

Answers

  • User-17257777 posted

    Hi Lisa,

    As bruce said, if you access Two() right after Index(), the TempData should be still valid, since it can be used in the subsequent request. If this is the case, you should change your TempData like below:

    public IActionResult Index()
    {
        List<string> lstStudent = new List<string>();
        lstStudent.Add("Debasis");
        lstStudent.Add("Samrat");
        lstStudent.Add("Rahul");
        TempData["StudentData"] = JsonConvert.SerializeObject(lstStudent);
        return View();
    }

    public IActionResult Two() { var modelData = JsonConvert.DeserializeObject<List<string>>(TempData["StudentData"].ToString()); TempData.Keep(); return View(modelData); }

    If it is not the case, I think session is the best choice.

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2020 3:33 AM

All replies

  • User-474980206 posted

    did you access action two() right after index()? if you went to any other action between the temp data cookie is cleared 

    if you want the value to resist, just use your own cookie.

    Wednesday, August 19, 2020 7:01 PM
  • User1596943402 posted

    Thank you for your response Bruce.  Yes, I want tempdata to persist even if I choose different action methods.  So does that mean my only option is to use either a session or a cookie?  Is there anything else that I can choose?  

    Wednesday, August 19, 2020 8:52 PM
  • User-17257777 posted

    Hi Lisa,

    As bruce said, if you access Two() right after Index(), the TempData should be still valid, since it can be used in the subsequent request. If this is the case, you should change your TempData like below:

    public IActionResult Index()
    {
        List<string> lstStudent = new List<string>();
        lstStudent.Add("Debasis");
        lstStudent.Add("Samrat");
        lstStudent.Add("Rahul");
        TempData["StudentData"] = JsonConvert.SerializeObject(lstStudent);
        return View();
    }

    public IActionResult Two() { var modelData = JsonConvert.DeserializeObject<List<string>>(TempData["StudentData"].ToString()); TempData.Keep(); return View(modelData); }

    If it is not the case, I think session is the best choice.

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2020 3:33 AM
  • User1596943402 posted

    Hello Jiadong, 

    This works perfectly!

    Many thanks for your help.

    Thursday, August 20, 2020 3:49 AM