0

I have a method defined like this:

public ActionResult MatchedBusinesses(List<Business> businesses)
    {
        if (businesses != null)
        {
            return View(businesses);
        }

        return View("NoMatchFound");
    }

Then, in my other method I have something similar to this one:

var list = results.AsEnumerable().OrderBy(b => Math.Abs(Convert.ToInt32(temp) - Convert.ToInt32(b.Zip))).Take(5).ToList();

return RedirectToAction("MatchedBusinesses", "Home", list);

The point is that, for the list variable I get the 5 entries that I select using the query. But, then I want to pass that result to my other method, which will be used in other method's view. The problem is, when I call the other method, the businesses parameter is always null. How can I solve the problem? Clearly, I'm not passing the parameter to my MatchedBusinesses method correctly. Any idea, how to solve the problem?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
tett
  • 595
  • 3
  • 13
  • 34
  • Possible duplicate of http://stackoverflow.com/questions/22505674/can-we-pass-model-as-a-parameter-in-redirecttoaction – Matt M Dec 15 '14 at 19:40

3 Answers3

1

You are using the overload of RedirectToAction where the 3rd parameter is object routeValues. Internally the method uses reflection to build the route values based on the names and the ToString() values of the objects properties.

It works only for properties that are value types, but for properties that are complex types, including collections, it will not bind because (in your case) the value is a string "List<YourAssembly.Business>" and a string cannot be bound to a collection.

You need to persist the collection before redirecting (e.g. database, session, TempData) and then retrieve the collection in the action result.

For example

var list = results.AsEnumerable()....
TempData["results"] = list;
return RedirectToAction("MatchedBusinesses", "Home");

public ActionResult MatchedBusinesses()
{
  List<Business> businesses = (List<Business>)TempData["results"];
}

but use TempData with caution (if the user refreshes the browser, the data will be lost). Its better to persist the information to the database with some key, and then pass the key as a route parameter to the MatchedBusinesses() method so that you can retrieve the data from the database.

0

Edit
What you're trying to do doesn't make much sense. You cannot, and should not, attempt to send large and/or complex objects, like a List, using Route. Instead you should use POST, or follow Stephen Muecke's suggestion in using TempData

However, here's how you can correctly send simple values using RouteValue

You pass parameters by using

return RedirectToAction("ActionName", "ControllerName", 
    new { paramName = paramValue });

Or if the target Action it's in the same controller

return RedirectToAction("ActionName", new { paramName = paramValue });

The parameter name, is optional. But using

return RedirectToAction("ActionName", new { paramName = paramValue });

Implies that the target action accepts a parameter with the name paramValue.

Here are all the overloads for RedirectToAction
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction%28v=vs.118%29.aspx

Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • This works, but then my parameter doesn't get the entries from my `list` variable. It still shows that the count of the parameter is 0. – tett Dec 15 '14 at 21:12
  • `RedirectToAction("MatchedBusinesses", "Home", new { businesses = list })` ? – Mihai Dinculescu Dec 15 '14 at 21:16
  • Yes, that's exactly what I have, but when I put a break point at `if (businesses != null)`, it shows me that `businesses` has count of 0, whereas my `list` had count of 5. – tett Dec 15 '14 at 21:33
  • What you're trying to do doesn't make much sense. You cannot, and should not attempt to send large and/or complex objects, like a List using Route. Instead you should use POST, or Stephen Muecke's suggestion. – Mihai Dinculescu Dec 15 '14 at 21:45
  • 1
    Read this: http://sampathloku.blogspot.com/2012/09/how-to-use-aspnet-mvc-tempdata-properly.html – Mihai Dinculescu Dec 15 '14 at 21:47
-2

Try wrapping your parameter in your return statement in a blank object variable like so:

return RedirectToAction("MatchedBusinesses", "Home", new { businesses = list });

All of the route values for an action have to be one parameter, so it's passed as an object, and then split into the various parameters of the receiving action. Even if you have only one param, it's still looking for an object to split.

Adurnari
  • 9
  • 5