mvc3如何在动作方法之间传递列表对象

时间:2013-12-17 16:32:26

标签: asp.net-mvc-3 list action selectlistitem

我在一个控制器中有一个List对象,我想知道如何将它传输到下一个控制器,例如这就是我所拥有的

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

        });

我怎样才能转移东西?我迷失了。

1 个答案:

答案 0 :(得分:0)

在NameList方法中,您可以添加以下行:

TempData["namelist"] = mynames;

然后在newList方法的顶部添加以下行:

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

访问TempData中的值后,将从TempData对象中清除它