【问题标题】:Is it possible to change URL when returning View in the same time ? That is without redirecting?是否可以在同时返回 View 时更改 URL?那是没有重定向?
【发布时间】:2011-07-20 15:53:30
【问题描述】:

在 ASP.NET MVC 中,我有一个看起来像这样的控制器:

public class MyController{
    public ActionResult Index(){
        return View(new MyModel());
    }
    [HttpPost]
    public ActionResult Index(MyModel model){
        //do something
        return View("RegistrationConfirmation", new ConfirmModel());
    }
    [HttpPost]
    public ActionResult RegistrationConfirmation(ConfirmModel model){
        //do something else
        return View();
    }
}

我想要的用户工作流程如下

  • GET 页面索引。返回视图Index。网址:~/My
  • POST 来自索引页面的数据 - 返回视图 RegistrationConfirmation 并将用户发送到右侧页面 ~/My/RegistrationConfirmation
  • RegistrationConfirmation 页面上用户POSTs 另一个数据,以便调用RegistrationConfirmation 来处理它们。

现在操作方法RegistrationConfirmation 永远不会被调用,因为在通过索引操作方法返回RegistrationConfirmation 视图后,URL 保持在~/My,所以第二个帖子由Index(MyModel) 操作方法而不是RegistrationConfirmation(ConfirmModel) 操作方法处理。

如何在发送视图的同时更改 URL,以便在 POST back 上调用与视图对应的控制器操作?或者有没有其他方法可以确保调用对应的控制器?


注意:在发布此问题之前,我已经阅读了 20 多个似乎与主题相关的问题。我不认为对其中任何一个的完美答案会给我解决方案。请在投票结束前正确阅读重复。

【问题讨论】:

  • Html.Action 方法应该让您灵活地选择要调用的正确操作方法?

标签: c# asp.net asp.net-mvc url redirect


【解决方案1】:

在RegisterationConfirmation视图中试试这个

您可以轻松添加应在 Html.BeginnForm 命令中定位的操作和控制器...

   <% using(Html.BeginForm("RegistrationConfirmation", "MyController")){%>

    //form elements

   <%}%>

您可以准确地定义要调用的动作和控制器

【讨论】:

  • 昨晚我遇到了类似的问题。这个答案是我修复它的方法。在 BeginForm 中,您可以指定任何控制器和任何操作来处理您的 POST。
  • 谢谢!工作。在我的情况下,我必须将 ID 添加到 BeginForm 的 url 的参数中,如stackoverflow.com/questions/20942926/… 中所述
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2013-10-08
  • 2014-10-31
  • 2019-06-17
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多