因此,如果我在第一个 Controller 中执行此操作:
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 );
}
}
在第二个 Controller 中,myObj 将正常运行,但 Address 将为空。
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
}
}
关于asp.net-mvc - 使用 RedirectToAction 时,routeValue 丢失引用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/718669/