0

Given an MVC view like so:

<form id="Form1">
    <label for="LastName">
        @Html.DisplayNameFor(m => m.LastName)
    </label>
    @Html.EditorFor(m => m.LastName)
    <button id="SubmitOne" onclick="SubmitOne_Click()">SubmitOne</button>
</form>

<form id="Form2">
    <label for="LastName2">
        @Html.DisplayNameFor(m => m.LastName2)
    </label>
    @Html.EditorFor(m => m.LastName2)
    <button id="SubmitTwo" onclick="SubmitTwo_Click()">SubmitTwo</button>
</form>

The javascript functions are irrelevant, this problem also occurs when they are empty or just have an alert.

When SubmitOne is clicked, everything works as expected. However, when SubmitTwo is clicked the whole page refreshes, which isn't supposed to happen. If I comment out Form1, then Form2 works fine.

What is going on here?

Update: Here's the javascript:

function SubmitOne_Click() {
    alert("One");
}

function SubmitTwo_Click() {
    alert("Two");
}

As a troubleshooting step, I stripped the view down to nothing but the form, and the script down to nothing as well. I do have references to Bootstrap, jqGrid, and jQuery in the _Layout, but nothing that currently uses them in this view.

Devin Goble
  • 2,639
  • 4
  • 30
  • 44

3 Answers3

1

It turns out that I needed to add a type attribute to my buttons:

<button type="button" id="SubmitTwo" onclick="SubmitTwo_Click()">SubmitTwo</button>

This prevented the button from doing a full submit of the form.

See also: How to prevent buttons from submitting forms

Community
  • 1
  • 1
Devin Goble
  • 2,639
  • 4
  • 30
  • 44
0

Having multiple forms on the page does not cause any issues unless they are nested, which is not your case. I bet the issue is indeed in the JavaScript.

Please show us relevant JavaScript code.

Xyz
  • 5,955
  • 5
  • 40
  • 58
Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
0

You don't have nested forms, You don't have same ID, You don't have same of anything. ( So that is good)

Please post your javascript code and also, be nice to peek at your razor form code along with your controller methods

Tom Stickel
  • 19,633
  • 6
  • 111
  • 113