简体   繁体   English

ASP.NET MVC表单:可以使用ENTER键提交,但不能提交按钮

[英]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. 我正在尝试在ASP.NET MVC中实现一个简单的搜索表单。 Right now my cshtml looks like this 现在我的cshtml看起来像这样

@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. 按提交按钮不会调用控制器,但按ENTER可以。 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. 我猜这意味着我的click事件被JavaScript或其他东西吞噬了,所以我试图暂停Chrome中的脚本执行,但意识到browserlink.js陷入了无限循环。 Is that the problem? 那是问题吗? Either way, how can I stop the infinite loop? 无论哪种方式,如何停止无限循环?

Browser doesn't know to use this button as submit. 浏览器不知道使用此按钮作为提交。

Change 更改

type="button"

To

type="submit"

Instead of setting the type as "button" 而不是将type设置为"button"

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

Change the type to submit 更改typesubmit

<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. 您还需要一个value属性。 This will be a reference to the Function name within the controller. 这将是对控制器内功能名称的引用。 w3 reference w3参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM