tags:

views:

229

answers:

2

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.

+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
+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
`http://localhost/Home/HomePage?msg='I can show any message I want:D'`
LukLed
hm, you got a point there =]. Never actually used it in a redirect. Never send strings as a value anyway =].
bastijn