user1238784
user1238784

Reputation: 2440

httpPost Form with two submit buttons for different actions in Asp.net mvc

On a razor view I have a form which has a certain action with a button to submit the form. Now I would like to add another button that post the form to a different action. These are buttons "Next" and "Previous". I have to perform different task but I need the same form to be posted. I already have the two actions ready, but I do not know how to post the form with two different actions depending on the button pressed. At the moment only the "Next" button works. Here is some code of the razor view:

<form asp-action="PasgStoreAndNext" method="post">
            <p>
                <label asp-for="Name">Name</label>
                <input asp-for="Name" value="@Model.Name"/>
            </p>
            <p>
                <label asp-for="Surname">Surname</label>
                <input asp-for="Surname" value="@Model.Surname"/>
            </p>
            <p>
                <label asp-for="DateOfBirth">DateOfBirth</label>
                <input asp-for="DateOfBirth" value="@Model.DateOfBirth"/>
            </p>
            <p>
                <label asp-for="Address">Address</label>
                <input asp-for="Address" value="@Model.Address"/>
            </p>
            <p>
                <label asp-for="Town">Town</label>
                <input asp-for="Town" value="@Model.Town"/>
            </p>
            <p>
                <label asp-for="PostalCode">PostalCode</label>
                <input asp-for="PostalCode" value="@Model.PostalCode"/>
            </p>
            <p>
                <label asp-for="Country">Country</label>
                <input asp-for="Country" value="@Model.Country"/>
            </p>
            <p>
                <label asp-for="Telephone">Telephone</label>
                <input asp-for="Telephone" value="@Model.Telephone"/>
            </p>
            <p>
                <label asp-for="PassportNr">PassportNr</label>
                <input asp-for="PassportNr" value="@Model.PassportNr"/>
            </p>
            <p>
                <label asp-for="Email">Email</label>
                <input asp-for="Email" value="@Model.Email"/>
            </p>
            <p>
                <label asp-for="Pwd">Password</label>
                <input asp-for="Pwd" type="password" value="@Model.Pwd"/>
            </p>
            <button type="submit">Next</button>
        </form>

Upvotes: 1

Views: 1703

Answers (1)

Shyju
Shyju

Reputation: 218722

You can use the formaction attributes on the button. If the formaction attribute value is specified, it overrides the action attribute of the button's form.

<form asp-action="PasgStoreAndNext" method="post">

   <!--Your form input elements--> 

   <button type="submit">Next</button>
   <button type="submit" formaction='@Url.Action("Previous","Home")'>Previous</button>

</form>

Upvotes: 3

Related Questions