简体   繁体   中英

Is it possible to change URL when returning View in the same time ? That is without redirecting?

In ASP.NET MVC I have a controller that looks somehow like this:

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();
    }
}

User's workflow that I'd like to have is following

  • GET page index. Returns view Index . URL: ~/My
  • POST data from Index page - returns view RegistrationConfirmation and send the user to right page ~/My/RegistrationConfirmation .
  • User POST s another data while on RegistrationConfirmation page so that RegistrationConfirmation gets called to process them.

Right now action method RegistrationConfirmation is never called because after returning RegistrationConfirmation view by Index action method URL stays ~/My so the second post is processed by Index(MyModel) action method not by RegistrationConfirmation(ConfirmModel) action method.

How can I change URL along with sending the View so that the controller action that corresponds to the view gets called on POST back ? Or is there any other way how to ensure that corresponding controller is called?


NOTE: I have really read more than 20 questions that seemed to be on topic before posting this one. I don't think perfect answer to any of them will give me solution. Please read properly before voting to close as duplicate.

try this one in the view RegistationConfirmation

you can easily add the action and the controller which should be targeted in the Html.BeginnForm command...

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

    //form elements

   <%}%>

with this you exactly define which action and controller is going to be called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM