且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MVC 5 asp.net,从控制器到视图的视图包不起作用

更新时间:2023-02-25 19:14:23

public ActionResult Test() {
 TempData["shortMessage"] = "MyMessage";
 return RedirectToAction("Details", new { id = theId});
}

public ActionResult Details {
 //now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
  ViewBag.TheMessageIs = TempData["shortMessage"].ToString();
  return View();
}

您必须这样做,因为当您重定向到另一个活动/视图时,视图包会失去其值

You have to do it like this since the viewbag looses its value when you redirect to another active / view