Nick Gilbert Nick Gilbert - 20 days ago 4
ASP.NET (C#) Question

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?

Answer

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

Change

type="button"

To

type="submit"
Comments