2

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?

3 Answers 3

7

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

Change

type="button"

To

type="submit"
Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively, you can use <button>Submit(or some other text)</button> to create a submit button. To prevent submit, add type="button" to the button tag (you can technically also add type="submit" to a button, but there's never really a reason to.
1

Instead of setting the type as "button"

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

Change the type to submit

Comments

-3

<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

2 Comments

formmethod isn't required if the form element already has a method attribute. This also doesn't fix the type="button" which is the real issue.
Yeah i agree with you. He should change type="submit"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.