0

I need to pass list of objects from one controller to another controller. i have read the similar questions's answers but nothing could help me. here is code of first controller :

[HttpPost]
        public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
        {
            switch (Action_Code)
            {
                case 1: 
                    {   //redirecting requesting to another controller 
                        return RedirectToAction("Quran_Loading", "Request_Handler",things);
                    }
               default:
                       ...
            }
        }

Request_Handler code :

public class Request_HandlerController : Controller
    {
        public ActionResult Quran_Loading(List<thing> thin)
        {...}
    }

but problem is that list in Quran_Loading method is null. any idea ?

tereško
  • 58,060
  • 25
  • 98
  • 150

4 Answers4

6

Passing List from controller to another is not possible in actions, Because RedirectToAction is HTTP request that u can't pass list to.

you can use one of three options ViewData, ViewBag and TempData for passing data from controller to View or another controller

you can check this reference here for more information about the difference between the three options.

[HttpPost]
public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
 {
      switch (Action_Code)
      {
          case 1: 
             {   
                    TempData["Things"] = things; 
                    // OR 
                    ViewBag.Things = things;
                    // OR 
                    ViewData["Things"] = things;

                    //redirecting requesting to another controller 
                    return RedirectToAction("Quran_Loading", "Request_Handler");
             }
           default:
                   ...
        }
    }

Request Handler

public class Request_HandlerController : Controller
{
    public ActionResult Quran_Loading()
    {
          List<thing> things = (List<thing>)TempData["Things"];
          // Do some code with things here
    }
}

Check this code and tell me if there is any question

Mohamed Salah
  • 959
  • 10
  • 40
  • 2
    When using RedirectToAction, only TempData will work, ViewBag and ViewData will not work, since they will be cleared. – Max Dec 24 '13 at 11:26
  • 1
    I think we can use `return Redirect("action_Name")` Or `return View("action_Name")` i think this will not clear the ViewBag and ViewData – Mohamed Salah Dec 24 '13 at 11:29
  • RedirectToAction can be used with TempData, TempData will not be cleared and is able to be used in view after using RedirectToAction, but ViewBag and ViewData will be cleared after. – Max Dec 24 '13 at 11:35
  • Yes a agree with this. I was talking about the other two options `ViewBag` and `ViewData`. `return View()` will not clear them. – Mohamed Salah Dec 24 '13 at 11:42
  • how to traverse tempdata in view ? @MohamedSalah –  Dec 31 '13 at 14:17
  • You can use `Tempadata` at view like `ViewData` @TempData["things"] this will represent the list you assigned in controller – Mohamed Salah Dec 31 '13 at 17:52
2

Look at TempData, I think it should solve your problem. RedirectToAction is HTTP request with 302 code and setting new location in headers.

ASP.NET MVC - TempData - Good or bad practice

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • @BaqerNaqvi Before return you fill `TempData` and in second action cast it to your model? Like here: http://stackoverflow.com/questions/12422930/using-tempdata-in-asp-net-mvc-best-practice – webdeveloper Dec 24 '13 at 11:06
2
    [HttpPost]
    public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
    {
        switch (Action_Code)
        {
            case 1: 
                {   //redirecting requesting to another controller 
                    TempData["Things"]=things; //transferring list into tempdata 
                    return RedirectToAction("Quran_Loading", "Request_Handler",things);
                }
           default:
                   ...
        }
    }


public class Request_HandlerController : Controller
{
    public ActionResult Quran_Loading()
    {
        List<thing> things=TempData["Things"] as  List<thing>;
     }
}

TempData

Lifecycle of tempdata is very short. The best scenario to use when you are redirecting to another view. Reason behind this is that redirection kill the current request and send HTTP status code 302 object to server and also it creates a new request.

1

TempData will not be cleared after using RedirectToAction, you should code as follow:

Controller:

TempData["things"] = (List<thing>)things;

View:

@if(List<thing>)TempData["things"] != null)
{
  <tr>
    <td>
      <ul>
        foreach(var item in (List<thing>)TempData["things"])
        {
          <li><b>@item.PropertyName</b></li>
        }
      </ul>
    </td>
  </tr>
}
Max
  • 12,622
  • 16
  • 73
  • 101