是否可以在调用重定向之前设置 ViewBag
?
我想要这样的东西:
@ViewBag.Message="MyMessage";
RedirectToAction("MyAction");
最佳答案
当你使用重定向时,你不能使用ViewBag
,而是使用TempData
public ActionResult Action1 () {
TempData["shortMessage"] = "MyMessage";
return RedirectToAction("Action2");
}
public ActionResult Action2 () {
//now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
ViewBag.Message = TempData["shortMessage"].ToString();
return View();
}
关于c# - 在重定向之前设置 Viewbag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497711/