2

I have a method...

[HttpPost]
public ActionResult Start(SomeViewModel someViewModel) { ... }

that based on some conditions returns things like return View("Invalid"), View("NotFound"), View("Run", anotherViewModel), etc. The problem is that no matter what view I present, the URL does not change to reflect the new controller/action. This poses a problem when my View wants to post to a different action. How can I fix this?

Brian David Berman
  • 7,514
  • 26
  • 77
  • 144
  • Returning a ViewResult (by calling View(...)) essentially returns a bunch of HTML in response to the current request. If you want to change the URL, that is, have the browser GET data from a new address, you should return a RedirectResult. That's what Serge's code does. – Rune Feb 06 '11 at 22:18

2 Answers2

11

If you want to change the URL, you need a redirection to the action associated with that URL, such as

[HttpPost] 
public ActionResult Start(SomeViewModel someViewModel) 
{
  ...
  return RedirectToAction("SomeOtherAction");
}

The action SomeOtherAction will in turn display the view.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • How would this work if you had used controller.excute to redirect from a helper class or the global.asax? – GP24 Jul 17 '15 at 14:27
8

The View(...) methods don't redirect, they simply render out the specific view on the current request. If you need to target a specific url in the form of your view, you can pass in the controller/action details to the form method:

Html.BeginForm("action", "controller")

... etc

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129