I have a webpage with a form, which looks kinda like this:
@using (Html.BeginForm("MyAction", "Controller", FormMethod.Post))
{
// html input fields here
// ...
// [SUBMIT]
}
When a user presses on the submit button, then the following function is called:
public ActionResult MyAction ( string id )
{
// default action
}
[HttpPost]
public ActionResult MyAction ( MyModel model )
{
// called when a form is submitted
}
Now my problem is, is that i have to add another form. But how can i tell which form was submitted? Because both will now end up in the second (HttpPost) method…
What would be a good way to separate both form actions? Please note that when a form is submitted, that i must stay on the same page. I can’t redirect myself to another page/controller.
If I understand your question correctly you will have a page with two forms in it.
As a first approach I will post each form to a different action of the same controller.
The first
The second
and then, a little refactoring on your two actions to follow the DRY principle.
If instead you need both form to post to the same action, then I will put a hidden input to let me know wich one was called.