所以如果我在第一个控制器中这样做:
public class AController:Controller { public ActionResult ActionOne() { MyObject myObj = new MyObject() myObj.Name="Jeff Atwood"; myObj.Age =60; myObj.Address = new Address(40,"Street"); return RedirectToAction("ActionTwo", "BController", myObj ); } }
在第二个控制器中,myObj会出来,但地址将为空.
public class BController:Controller { public ActionResult ActionOne(MyObject obj) { //obj.Address is null? } }
这是预期的吗?任何方式呢?
您可以使用TempData存储两个请求之间可用的对象.在内部,默认实现使用Session.
public class AController:Controller { public ActionResult ActionOne() { MyObject myObj = new MyObject() myObj.Name = "Jeff Atwood"; myObj.Age = 60; myObj.Address = new Address(40, "Street"); TempData["myObj"] = myObj; return RedirectToAction("ActionTwo", "BController"); } } public class BController:Controller { public ActionResult ActionTwo() { MyObject myObj = TempData["myObj"] as MyObject; // test if myObj is defined. If ActionTwo is invoked directly it could be null } }