I’m trying to pass an object from one controller action to another. The object that I’m passing around looks more or less like this:
public class Person
{
public string Name { get; set; }
public List<PhoneNumber> PhoneNumbers {get; set; }
public List<Address> Addresses { get; set; }
}
My Controller looks like this:
public class DialogController : Controller
{
public ActionResult Index()
{
// Complex object structure created
Person person = new Person();
person.PhoneNumbers = new List();
person.PhoneNumbers.Add("12341324");
return RedirectToAction("Result", "Dialog", person);
}
public ActionResult Result(Person person)
{
string number = person.PhoneNumbers[0].ToString();
return View();
}
}
The result method fails with a null pointer exception since the PhoneNumbers list is suddenly null after invoking the Result action with the RedirectToAction() method.
Has anyone seen this type of behavior before?
Cheers,
Peter
I agree with @Dennis — unless you want the Url to change, then you’ll have to think of something else. 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, although you could also store the item in the database, pass the id to the Result method, and reconstitute it from there.