tags:

views:

98

answers:

2

Hi,

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

+4  A: 

Do you really need to Redirect to the other action? RedirectToAction causes a brand new http request, which is why TempData works. Couldn't you just call the Result action directly like this?

public ActionResult Index()
{
    // Complex object structure created
    Person person = new Person();
    person.PhoneNumbers = new List();
    person.PhoneNumbers.Add("12341324");

    return Result(person);

}

Edit Unless your app is doing more than what you've shown in the question, it doesn't look like you really need the Index action. You could move the code that creates a new person into a private CreatePerson method. In your Result action if person is null, then call the CreatePerson method. The Index action could be eliminated altogether, but that would require modifying your routes. Or just let return RedirectToAction("Result", "Dialog"); be the only line of code in your Index action.

Actually, following MVC separation of concerns, that CreatePerson method should probably be a method inside your model code. The controller shouldn't need to contain the logic of creating a new Person. That really belongs in the model.

Dennis Palmer
Hi Dennis,Thanks for the answer. You are totally right about the creation of the Person objects.Thanks!
Aquaboy
A: 

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.

tvanfosson
Hi T,I ended up using the TempDaata trick to transfer objects from one action to the other. Thanks for clearing up how RedirectToAction works. That was the real confusing part for me.
Aquaboy