AppNove
AppNove

Reputation: 73

RedirectToRoute with Parameters Objects

I am in controller A and is using redirectToRoute to get to controller B where I am passing two object parameters but I notice these values are null on redirection.RedirectToRoute does not allow the passing of objects?????

Upvotes: 1

Views: 7079

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

When redirecting you can only pass simple scalar values and not complex objects. Example:

public ActionResult Index()
{
    SomeModel model = ...
    return RedirectToAction("Foo", new 
    {
        Prop1 = model.Prop1,
        Prop2 = model.Prop2,
        Prop3 = model.Prop3,
    });
}

public ActionResult Foo(SomeModel model)
{
    // model.Prop1, model.Prop2 and model.Prop3 will be correctly bound here
    ...
}

Another possibility is to persist the object to your datastore and pass only the corresponding id:

public ActionResult Index()
{
    SomeModel model = ...
    string id = Repository.Save(model);
    return RedirectToAction("Foo", new { id = id });
}

public ActionResult Foo(string id)
{
    // fetch the model from repository:
    SomeViewModel model = Repository.Load(id);
    ...
}

Upvotes: 4

Related Questions