1

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

Vishal
  • 12,133
  • 17
  • 82
  • 128

1 Answers1

4

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.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • hmm..so in this example i tried doing-........ return RedirectoAction("SomeAction",new {vm=vm}) and also....... return RedirectoAction("SomeAction",new MyViewModel{idvm.id,obj= new SomeClass {phone=vm.phone,zip=vm.zip} ) but didnt work ? – Vishal Jul 14 '10 at 19:41
  • I'm going to say this again, slowly: The second argument to `RedirectToAction` **is not a view model.** It changes the URI. **That is all.** – Craig Stuntz Jul 14 '10 at 19:44
  • 1
    got it...you could have said "tempdata" slowly..:) – Vishal Jul 14 '10 at 20:10
  • OR If you can bypass the action.. return View("SomeView", vm) ... which will pass the viewmodel. – Paul Zahra May 30 '14 at 14:32