Nick Gilbert
Nick Gilbert

Reputation: 4240

ASP.NET MVC Form: Can submit with ENTER key but not Submit button

I'm trying to implement a simple search form in ASP.NET MVC. Right now my cshtml looks like this

@using (Html.BeginForm("SearchDemo", "Home", FormMethod.Post))
{
    <div>
        Last Name:<br>
        <input type="text" id="nameToFind" name="nameToFind">

        <input type="button" id="submitId" name="submit" value="submit" />

    </div>
}

And my controller looks like this

[HttpPost]
public ActionResult SearchDemo(string nameToFind)
{
    return RedirectToAction("Register", "Account"); //Only redirecting for now to test
}

Pressing the submit button does not call the controller but pressing ENTER does. I'm guessing that means my click event is getting eaten by JavaScript or something so I tried to pause script execution in Chrome but realized browserlink.js is stuck in an infinite loop. Is that the problem? Either way, how can I stop the infinite loop?

Upvotes: 2

Views: 4967

Answers (3)

Sravan Dudyalu
Sravan Dudyalu

Reputation: 185

Instead of setting the type as "button"

<input type="button" id="submitId" name="submit" value="submit" />

Change the type to submit

Upvotes: 1

akasoggybunz
akasoggybunz

Reputation: 344

<input type="button" id="submitId" name="submit" value="submit" formmethod="post" value="Submit using POST"/>

You need a formmethod of post in your input. So that the button knows to post back. You also need a value attribute. This will be a reference to the Function name within the controller. w3 reference

Upvotes: -3

Orel Eraki
Orel Eraki

Reputation: 12196

Browser doesn't know to use this button as submit.

Change

type="button"

To

type="submit"

Upvotes: 7

Related Questions