user3119163
user3119163

Reputation:

how to pass list of objects from one controller to another controller?

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 ?

Upvotes: 0

Views: 11367

Answers (4)

Max
Max

Reputation: 13338

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>
}

Upvotes: 1

Vishal Dixit
Vishal Dixit

Reputation: 31

    [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.

Upvotes: 2

Mohamed Salah
Mohamed Salah

Reputation: 975

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

Upvotes: 6

webdeveloper
webdeveloper

Reputation: 17288

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

Upvotes: 2

Related Questions