在我的MVC项目中,我有两个表单在提交时我想重定向到同一个ThankYou页面。
在2个表单的控制器上,我在ViewBag中保存了ThankYou页面的文本。
在Controller上,我将ViewBage设置为:
# Alternatively: Plot with points and line
plot(as.numeric(x1), df$Freq, type = 'b',
xaxt = "n",
xlab = "Month",
ylab = "Freq");
axis(1, at = as.numeric(x1), labels = as.character(x1));
在ThankYou View上,我明白了:
if (result == "success")
{
ViewBag.ThankYouText = "We have received your Contact us request."
return RedirectToAction("Index", "ThankYou",ViewBag);
}
但我在ThankYou页面上一无所获。
我是纯MVC的新手,所以我一定做错了什么,不知道吗?
答案 0 :(得分:1)
您无法将ViewBag值从一个控制器传递给另一个控制器。它应该用于将数据从控制器传递到仅查看。
如果要将数据从控制器传递到控制器,可以使用TempData。
按以下方式更改您的控制器:
if (result == "success")
{
TempData["ThankYouText"] = "We have received your Contact us request."
return RedirectToAction("Index", "ThankYou");
}
更改您的观点如下:
<p>@TempData["ThankYouText"]</p>
答案 1 :(得分:0)
像这样改变代码
TempData["ThankYouText"] = "We have received your Contact us request.";
return RedirectToAction("Index", "your controller");
索引方法中的
public ActionResult Index()
{
ViewBag.ThankYouText = TempData["ThankYouText"]; return View();
}
并在ViewBag.ThankYouText之前使用@关键字