views:

1695

answers:

2

Say I have a controller with an Index Method and a Update Method. After the Update is done I want to redirect to Index(). Should I use return RedirectToAction("Index") or can I just call return Index()? Is there a difference?

public ActionResult Index()
{
  return View("Index", viewdata);
}

public ActionResult Update()
{
  // do updates
  return RedirectToAction("Index"); or
  return Index();
}
+7  A: 

Use the redirect otherwise the URL on the client will remain the same as the posted URL instead of the URL that corresponds to the Index action.

tvanfosson
thx, I did not think of that one
TT
+2  A: 

Other things to consider:

  • Redirect action after a POST will act more nicely when the user clicks Refresh button, since they won't be prompted to resend data to server.

  • Form data will be lost with the redirect action unless you maintain them explicitly through, say, TempData. Without doing this, your form controls won't have any value after a POST, which may be undesirable in some cases.

Buu Nguyen