我在控制器A中并使用redirectToRoute到达控制器B,我传递了两个对象参数,但我注意到这些值在重定向时为空.RedirectToRoute不允许传递对象?????
答案 0 :(得分:4)
重定向时,您只能传递简单的标量值而不能传递复杂的对象。例如:
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
...
}
另一种可能性是将对象持久保存到数据存储区并仅传递相应的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);
...
}