I want to do something, like edit my email address in a profile page, then return to the homepage saying it was successful - just like stackoverflow does when you receive answers or earn a new badge.
views:
229answers:
2
+2
A:
Try using TempData:
public ActionResult PostUserEmail(string email)
{
TempData["Message"] = 'Email address changes successfully';
return RedirectToAction("HomePage");
}
public ActionResult HomePage(string email)
{
ViewData["Message"] = TempData["Message"];
return View();//Use ViewData["Message"] in View
}
TempData["Message"]
will be still there after redirection. TempData persists to next request, then it is gone.
LukLed
2010-04-21 06:10:22
+1
A:
I'm not using MVC2 but in MVC1 it is possible to do something like:
return RedirectToAction("HomePage", new { msg = "your message" });
public ActionResult HomePage(string msg)
{
// do anything you like with your message here and send it to your view
return View();
}
Though it might be a little too much for sending a simple message. You can send any object you want using this method. I usually use this method since I generally do not like using string based lookups (TempData["stringLookup"], ViewData["tringLookup"]) since I make a lot of typos. This way you have the advantage of intellisense.
bastijn
2010-04-21 06:28:08
`http://localhost/Home/HomePage?msg='I can show any message I want:D'`
LukLed
2010-04-21 06:40:38
hm, you got a point there =]. Never actually used it in a redirect. Never send strings as a value anyway =].
bastijn
2010-04-21 07:04:48