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

。_饼干妹妹 提交于 2019-12-25 04:39:08

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/3248963/the-value-for-a-object-inside-a-viewmodel-lost-on-redirect-to-action-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!