35

I want to pass object in RedirectToAction. This is my code:

RouteValueDictionary dict = new RouteValueDictionary();
            dict.Add("searchJob", searchJob);
            return RedirectToAction("SearchJob", "SearchJob", dict);

where searchJob is instance of SearchJob. But I don't get data on SearchJob action method. Instead I get querystring of searchJob = Entity.SearchJob. Please help me. What am I doing wrong?

Jaggu
  • 6,298
  • 16
  • 58
  • 96
  • 1
    Did you look at the docs for `RedirectToAction`? – bzlm Sep 29 '11 at 13:15
  • What's the controller that has SearchJob action? Is it the same controller? – Ufuk Hacıoğulları Sep 29 '11 at 13:18
  • No SearchJob is the controller and it has action named SearchJob. – Jaggu Sep 29 '11 at 13:19
  • 2
    @bzlm: I haven't looked at the docs. But I found answer to my question. I stored in TempData. Kind of little hack I feel but it works. Thanks anyways for the negative vote :) – Jaggu Sep 29 '11 at 13:20
  • 3
    +1 because I hate when someone votes someone down and doesn't own up to it. –  Sep 29 '11 at 13:26
  • possible duplicate of [RedirectToAction(..) with complex deep object fails](http://stackoverflow.com/questions/1352015/redirecttoaction-with-complex-deep-object-fails) – mateuscb Mar 05 '14 at 17:49

4 Answers4

49

You can not pass classes to the redirected actions like that. Redirection is done by means of URL. Url is a string, so it can not contain classes (serializing objects to url is really out of logic here)

Instead, you could use TempData

TempData["searchJob"] = searchJob;
return RedirectToAction ...;

and in Action redirected

Entity.SearchJob = (Entity.SearchJob)TempData["searchJob"] ;

After executing of the code above, TempData will not contain searchJob anymore. TempData is generally used for single time reading.

But I do not like the way above. If I were in your place and wanted to search jobs by name, I would add route parameters like

RouteValueDictionary dict = new RouteValueDictionary();
dict.Add("searchJobName", searchJob.JobName);

and receive it to action via parameter

public ActionResult SearchJob(string searchJobName)
{
... do something with the name
}

This way, you get better user and HTTP friendly URL and from the Action point of view, it would get all the parameters it needs from outside. This is better for testing, maintenance, etc.

Maksym Labutin
  • 561
  • 1
  • 8
  • 17
archil
  • 39,013
  • 7
  • 65
  • 82
30

You might try:

return RedirectToAction("SearchJob", "SearchJob", new RouteValueDictionary(searchJob))

Passing the searchJob object into the RouteValueDictionary constructor will decompose the searchJob object and pass each property of the SearchJob class as a top-level route value.

With the default model binder, an action defined as:

public ActionResult SearchJob(SearchJob searchJob)

Will receive a fully re-hydrated SearchJob object.

Charlie
  • 8,530
  • 2
  • 55
  • 53
2

You can not pass classes to RedirectToAction method, if you want to pass an entire object in a querystring or via POST you can serialize the object using XML or JSON and deserialize the object in the receiver controller. If you use this approach to be careful on the size of the object serialized.

Massimo Zerbini
  • 3,125
  • 22
  • 22
-4

Try to use Cross-Page Posting you can determin Prevoiuse page type, and use it object.

Tomasz
  • 38
  • 1