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?
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
TempDataand in Action redirected
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
and receive it to action via parameter
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.