这是控制器
public ActionResult Test() {
@ViewBag.TheMessageIs = "this is the message";
return RedirectToAction("Details", new { id = theId});
}
在名为详细信息的操作的视图上,我将检查是否有要显示和显示的ViewBag:
@{
if(ViewBag.TheMessageIs != null){
@ViewBag.TheMessageIs
}
}
但是这里重定向对页面工作正常,它没有显示我在ViewBag中存储的消息.MessageIs
感谢
答案 0 :(得分:2)
基本上你正在做的是从Details
方法调用方法Index
,因为你已经用id
重载了你的详细信息操作,所以也要传递消息:
public ActionResult Index()
{
//ViewBag.TheMessageIs = "this is the message";
return RedirectToAction("Details", new { id = 1, TheMessageIs = "this is the message" });
}
public ActionResult Details(int id, string TheMessageIs)
{
ViewBag.TheMessageIs = TheMessageIs;
return View();
}
然后在“详细信息”视图中,您可以像这样访问该属性:
@ViewBag.TheMessageIs
答案 1 :(得分:1)
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();
}
你必须这样做,因为当你重定向到另一个活动/视图时,viewbag会丢失它的值