In ASP.NET MVC2 is there a way of returning a redirectToAction while passing a parameter?
I want to do something, like edit my email addr开发者_JAVA技巧ess 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.
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.
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.
精彩评论