2

My MVC4 project uses the RedirectToAction() to pass values to another controller.

The problem is, it passes null instead of a value

    [HttpGet]
    public ActionResult MyProduct(product prod)
    {
        return RedirectToAction("Index", "MyController", new { prod = prod});
    }

This accurately redirects to my MyController which is

    public ActionResult Index(Product prod)
    {
        // prod is null :(
    }

I put on a watch in the MyProduct controller, the prod object has a value. The properties do not have values :( it is null

Why does it become null when I pass the object between controllers?

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • possible duplicate of [Passing a model object to a RedirectToAction without polluting the URL?](http://stackoverflow.com/questions/13020202/passing-a-model-object-to-a-redirecttoaction-without-polluting-the-url) – Erik Philips Dec 19 '14 at 20:06

2 Answers2

4

You mistyped you parameter name that you are expecting at your action change it to prod because the prod is a part of RouteValueDictionary which will be collected by the same parameter as defined askey in the dictionary

return RedirectToAction("Index", "MyController", new { prod = prod});

Update

You cannot pass a model object via route parameters via RedirectToAction if I understood your question correctly, instead you can pass specific properties required to be send or you can use the TempData

You can't send data with a RedirectAction. That's because you're doing a 301 redirection and that goes back to the client.

When you redirect you mainly have the query string as a means to pass data to the other request. The other approach is to use TempData which stores data away on the server and you can retrieve it on the next request. By default TempData uses Session.

Consider this image for getting the ways of passing data between Model-View-Controller enter image description here

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • I am so sorry, that was a typo. Really sorry. Updated post – MyDaftQuestions Dec 19 '14 at 19:57
  • Can you elaborate more on tempdata... I don't see why I can't pass a reference type :( Or is it as simple as it will only accept value types in the parameters of a RedirectToAction ? – MyDaftQuestions Dec 19 '14 at 20:05
  • @MyDaftQuestions please check my updated answer i will add more to it – Tushar Gupta Dec 19 '14 at 20:07
  • Thank you so very much. Although clearly the correct answer (i guess my question will be closed eventually for being a dupe) this explains it well. using TempData feels like a hack but, if it works... – MyDaftQuestions Dec 19 '14 at 20:08
  • 1
    The bit about it being a 301 is amazingly clear. Thank you. This is really a super answer, well explained, clear and very useful. – MyDaftQuestions Dec 19 '14 at 20:10
  • 1
    closed as being a dupe :) – MyDaftQuestions Dec 19 '14 at 20:11
  • Of course you can pass a model object via route parameters - [refer docs](http://msdn.microsoft.com/en-us/library/dd460311(v=vs.118).aspx). Internally, a route value dictionary is generated based on the `.ToString()` value of each property in the model. But it only works for properties that are value types (properties that are collections or complex objects wont be bound). Having said that, its not a good idea to do it. –  Dec 19 '14 at 23:55
0

Your function parameter name doesn't match in your RedirectToAction().

You should change it like this:

return RedirectToAction("Index", "MyController", new { prod = prod});
Joce Pedno
  • 334
  • 1
  • 7