0

I was reading up on temp data and wanted to know if you can use tempdata with two separate keys and add more than one value to those. I.e.

TempData["Id"] = "1";

TempData["Name"] = "Bob";

then we have another set of values to add in the tempdata:

TempData["Id"] = "2";
TempData["Name"] = "Jill";

Using those in an action method:

public ActionResult Index()
{

    TempData["Id"] = 1;
    
    TempData["Name"] = "Bob";

    TempData["Id"] = 2;
    
    TempData["Name"] = "Jill";
}

then accessing that in another action method without having it to override the values of the keys and just give the last set of id and name:

public ActionResult About()
{

    int id;
    string name;

    if (TempData.ContainsKey("Id") && TempData.ContainsKey("Name"))
    {
         id = Convert.ToInt32(TempData["Id"]);
         name = TempData["Name"].ToString();
    }

}

Is there a way to display both Id's for Bob and Jill and both of their names when accessing it in the About() action method without just only getting 2 for ID and Jill for name, returned?

MarkCo
  • 810
  • 2
  • 12
  • 29

1 Answers1

1

This code was tested using VS 2019 and Newtonsoft.Json nuget package

you can keep a complex data in tempdata this way

public class IdName
{
 public int Id {get;set;}
public string Name {get; set;}
}

you can use it likes this


var idNames = new List<IdName> { 
new IdName { Name = "Bob", Id = 1 }, 
new IdName { Name = "Jill", Id = 2 } 
};
TempData["IdNames"] = JsonConvert.SerializeObject(idNames);

and your action

public ActionResult About()
{

if ( TempData.ContainsKey("IdNames") ) 
{
  List<IdName> IdNames =  JsonConvert.DeserializeObject<List<IdName>>( TempData["IdNames"].ToString());

    foreach( item in IdNames)
    {

        var id= item.Id;
        var name= item.Name

         // your code
    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • This is solid! Thank you for the detailed solution, glad it is possible! – MarkCo Aug 08 '21 at 21:24
  • When debugging the `About()` method, it tells me.... `InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer' cannot serialize an object of type 'System.Collections.Generic.List`1[TestApp.Models.IdName]'.` – MarkCo Aug 08 '21 at 22:02
  • @MarkCo Sorry about this. I usually test before posting. I fixed the code. – Serge Aug 09 '21 at 01:27
  • No worries I appreciate it! Inside of the for the loop of the `About()`, I do `item.Id = Convert.ToInt32(TempData["IdNames"]);` `item.Name = TempData["IdNames"].ToString();` and whenever I hit the first line `item.id` it tells me System.FormatException: 'Input string was not in correct format'. I looked up the error online and saw that a solution to this was to do `item.Id = 1 * Convert.ToInt32(TempData["IdNames"]);` but when I tried this I still get the same error. I do not understand why it is saying that if Id is type int and I am converting it to int in the forloop. – MarkCo Aug 09 '21 at 05:49
  • @MarkCo You just using it wrong way. Inside of the loop you have to use var id= item.Id; var name= item.Name. item doesn't have any setter, you can only read it. Check my updated unswer – Serge Aug 09 '21 at 10:43
  • I see, that makes a lot of sense now. Thank you for your explanation and help! – MarkCo Aug 09 '21 at 13:40