开发者

ASP.NET MVC ActionResult View() not changing url

开发者 https://www.devze.com 2023-02-09 11:40 出处:网络
I have a method... [HttpPost] public ActionResult Start(SomeViewModel someViewModel) { ... } that based on some conditions returns things开发者_运维技巧 like return View(\"Invalid\"), View(\"NotFou

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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消