MegaTron MegaTron - 3 years ago 95
ASP.NET (C#) Question

Redirect to action and passing reference type

I have a

Filter
type:

public class Filter
{
public int FilterId { get; set; }
public ICollection<int> ModuleIds { get; set; }
}


I need to redirect to action and pass instanse of this type:

public ActionResult Navigate
{
var filter = new Filter()
{
FilterId = 5,
ModuleIds = new List<int>() { 6 };
}

return RedirectToAction("list", "user", filter);
}


Here is the target action :

public ActionResult List(Filter filter)
{
var filterId = filter.FilterId; // = 5
// but filter.ModuleIds.Count = 0
}


In a result I got empty
filter.ModuleIds
. So, there is a issue only with reference type.

How can I pass
filter
with all reference properties without using
TempData
?

Also I tried to use but no results

return RedirectToAction("list", "user", new RouteValueDictionary(filter);


and

return RedirectToAction"list", "user", new { filter = filter} );

Answer Source

You can't pass reference types. The reason is that RedirectToAction doesn't serialize the data, it simply iterates over the properties in the route values object constructing a query string with the keys being the property names and the values the string representation of the property values. If you want to have the Url change, then using TempData is probably the easiest way to do this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download