Lucas
Lucas

Reputation: 6729

RedirectToAction not passing all parameters

I have these two methods in the same Controller, the first passing two parameters to the second.

When debugging, the list (model.RequestedProducts) passed is correct (not empty) but on the second method, only idOR is read correctly, List<OCS> RequestedProducts is empty.

    [HttpPost]
    public ActionResult Index(int idOR, ViewModel model, string add, string remove, string send)
    {
        //...
                return RedirectToAction("Done", 
                                         new { idOR = idOR, 
                                         RequestedProducts = model.RequestedProducts});       
    }

    public ActionResult Done(int IdOR, List<OCS> RequestedProducts)
    { ...

What am I missing?

Is there maybe a better way to do it? (other than Redirect to Action)

Thank you

Upvotes: 0

Views: 1684

Answers (1)

David Glenn
David Glenn

Reputation: 24522

When you use RedirectToAction you are returning a message to the client to request a new URL probably something like /controller/action/id. You Routes will define how the URL is formed. I am guessing you have the default route defined and in your case MVC has no way of knowing how to deserialise your RequestedProducts type into a URL and then bind it back into a List type.

Instead you could use the TempData object to pass data between to Action requests.

The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out.

This MSDN article explains it all.

Upvotes: 1

Related Questions