mvc3 how to transfer list objects between action methods

173 views Asked by At

I have a List object in 1 controller and I was wondering how can I transfer it to the next controller for example this is what I have

public ActionResult namelist()
    {
        var mynames = new List<everyname>
       {
           new everyname{ firstname ="john", Lastname= "henny"},                     
              new everyname{ firstname = "bob", Lastname = "cosso"},  
      new everyname{ firstname = "bill", Lastname = "luther"},
      new everyname{ firstname ="mike", Lastname= "jones"}

       };
        return View(mynames);
    }

 // how can i transfer the above list with information into this action below

         public ActionResult newlist()
    {
       // i tried transfering the list here and it did not work
        var namelist = new namelist()
        IEnumerable<SelectListItem> items =  namelist.Select(c => new SelectListItem
        {
            Value = namelist,
            Text = namelist

        });

how can i transfer things? i am lost on this.

1

There are 1 answers

0
HitLikeAHammer On

In the NameList method you can add this line:

TempData["namelist"] = mynames;

then at the top of the newList method add this line:

var namelist = Tempdata["namelist"] as List<everyname>;

Once you access a value in TempData it is purged from the TempData object