Search code examples
asp.net-mvc-2viewmodelredirecttoaction

The value for a object inside a viewmodel lost on redirect to action in asp.net mvc 2.0?


I have a view model -

public class MyViewModel
{
   public int id{get;set;};
   Public SomeClass obj{get;set;};
}
public class SomeClass
{
   public int phone{get;set;};
   public int zip{get;set;};
}

So on my controller when I post back MyViewModel it has all the values for all the fields...but when I do

return RedirectoAction("SomeAction",vm);//where vm->MyViewModel object that has all values...

it loses the values for SomeClass object?...can anyone please help me out


Solution

  • The second argument to RedirectToAction is route values, not a view model.

    So if you do:

    return RedirectoAction("SomeAction", new {Foo = "Bar"});
    

    Then, with the default model binding, you'll redirect to this URI:

    http://site/ControllerName/SomeAction?Foo=Bar
    

    Remember how a redirect works over the wire. You can't pass a model. You can only change the URI.