5

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 POSTs 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.

Rasto
  • 17,204
  • 47
  • 154
  • 245
  • Html.Action method should give you the flexibility to proper action method you want to invoke? – J.W. Mar 22 '11 at 16:53

1 Answers1

3

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

nWorx
  • 2,145
  • 16
  • 37
  • I had a similar problem last night. This answer is how I fixed it. In the BeginForm you can specify any controller and any action to process your POST. – Pete Mar 22 '11 at 16:49
  • Thanks! Worked. In my case I had to add an ID to the parameters of the url of the BeginForm as described in https://stackoverflow.com/questions/20942926/pass-multiple-parameters-in-html-beginform-mvc4-controller-action – Peheje Sep 18 '18 at 07:48