How to pass a List from one page to another.

Viewed 1763

I'm having this issue and i'm stumped. I'm able to pass and received single object but If I try to pass a list, the data is not going to the second page.

Here is my OrderList Page

  [BindProperty]
    public IList<Orders> Orders{ get; set; }
 public ActionResult OnPost()
        {
//get modified data. 
var orderList = Orders
 return RedirectToPage("/ConvertToCsv", orderList );
       }

on my ConvertToCsv page. 
  public void OnGet(IList<Orders> orderList )
        {
//do something with list. 
}

But the orderlist on OnGet is null. 
I tested by passing a single record like
 public ActionResult OnPost()
        {

 return RedirectToPage("/ConvertToCsv", new{orderId= "test"});
       }

  public void OnGet(string orderId )
        {
//This works.  
}

What am I doing wrong?

1 Answers

You can't pass complex objects as route data. The route data feature only supports simple objects like int and string. If you want to retain more complex objects across requests, you need to use Sessions or TempData (backed by session state).

TempData is probably the better option in this case because the item is removed from memory once you access it.

Further reading:

TempData: https://www.learnrazorpages.com/razor-pages/tempdata
Sessions: https://www.learnrazorpages.com/razor-pages/session-state
State Management in Razor Pages: https://www.learnrazorpages.com/razor-pages/state-management

Related