I'm redirecting from one controller to another, and sending over an object in the route values but the object is coming back as null:
class Person {
string Name {get; set;}
}
FirstController {
ActionResult something() {
Person p = new Person() { Name = "name" };
return redirectToAction("AddPerson", "Second", new { person = p });
}
}
SecondController {
ActionResult AddPerson(Person person) {
//I'm hitting this action method but the "person" object is always null for some reason
//add person
}
}
is it because passing objects around is not allowed?
I tried changing the parameter to a string rather than a Person
object, and sending over just the person.Name
and it sent it over... but why?
You cannot pass a complex object via the query string like that using the built in utilities.
You should send a reference to that object (something like an ID) where you can re-look it up on the other end. This only works if the Person is stored somewhere though (session, database, etc).
Other than that, you would have to send each propriety of the object individually and reconstruct it. You could also look into something like this as well: How do I serialize an object into query-string format? and maybe build your own utility that serializes the object. But you would still need to reconstruct it on your receiving method since this would not be done automatically.
A posted form can also map to a model defined in the parameters of an action, but that doesn't look like what you are trying to do.