ASP.NET MVC ActionResult View() not changing url

大城市里の小女人 提交于 2019-12-24 02:44:16

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4916468/asp-net-mvc-actionresult-view-not-changing-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!