On my MVC project I have two forms that on submit I want to redirect to the same ThankYou page.
On the controllers of the 2 forms I saved in the ViewBag the text for the ThankYou page.
On the Controller I set the ViewBage like this:
if (result == "success")
{
ViewBag.ThankYouText = "We have received your Contact us request."
return RedirectToAction("Index", "ThankYou",ViewBag);
}
And on the ThankYou View I get it:
<p>ViewBag.ThankYouText</p>
But I keep getting nothing on my ThankYou page.
I'm new to pure MVC so I must have done something wrong, any idea?
You can not pass ViewBag value from one controller to other. It is supposed to use for passing data from controller to view only.
If you want to pass data from controller to controller you can use TempData.
Change your controller as below:
if (result == "success")
{
TempData["ThankYouText"] = "We have received your Contact us request."
return RedirectToAction("Index", "ThankYou");
}
Change your view as below:
<p>@TempData["ThankYouText"]</p>