Reputation: 5862
The layout of this ASP.NET MVC application looks like the following (this is a mobile web app):
After each of the form submits, I'd expect the next page's view to appear and submit accordingly. But, what's really happening is on Page 1 when I hit the submit button, it shows Page 2's view, but now that I'm on Page 2 and I hit submit on that page, it calls the Action of Page 1's HttpPost
.
What would cause Page 2's submit to call Page 1's HttpPost
Action?
Here is the basic idea of what I'm doing:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel myModel)
{
return View("Page2", myModel);
}
[HttpPost]
public ActionResult Page2(MyModel myModel)
{
return View("Page3", myModel);
}
[HttpPost]
public ActionResult Page3(MyModel myModel)
{
if (ModelState.IsValid)
{
// do something and complete
}
return RedirectToAction("Index", "Home");
}
This is obviously oversimplified, but you should get the idea. The Page2
's View gets rendered no problem. But on that page when the submit button is hit, it goes right back to the HttpPost
of Index()
.
Any ideas?
Upvotes: 0
Views: 61
Reputation: 150313
The reason why it happens is because when you use mvc's BeginForm
HTML helper it submits to
the rendering action, not to the action that equals to the view name, but you can fix it!
Two options:
First: Set the values for the controller and action in the HTML helper:
@BeginForm("Page3", "controllerName")
Second: redirect to the action instead of returning the view:
[HttpPost]
public ActionResult Index(MyModel myModel)
{
return RedirectToAction("Page2", myModel);
}
[HttpPost]
public ActionResult Page2(MyModel myModel)
{
return RedirectToAction("Page3", myModel);
}
Upvotes: 1