简体   繁体   English

表单不在ASP.NET MVC中提交值

[英]Form doesn't submit values in ASP.NET MVC

I am using ASP.NET MVC 5. Here, when i click on the search bar it calls the java script function, and when i provide the searchString value to the form, and click SUBMIT, it doesn't submit anything. 我正在使用ASP.NET MVC5。在这里,当我单击搜索栏时,它将调用Java脚本函数,并且当我向表单提供searchString值并单击“提交”时,它不提交任何内容。 Why is that? 这是为什么?

<script>
    $(function () {
        $('a[href="#search"]').on('click', function (event) {
            event.preventDefault();
            $('#search').addClass('open');
            $('#search > form > input[type="search"]').focus();
        });

        $('#search, #search button.close').on('click keyup', function (event) {
            if (event.target == this || event.target.className == 'close' || event.keyCode == 27) {
                $(this).removeClass('open');
            }
        });
    });
</script>

View: 视图:

<body>
<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @if (Request.IsAuthenticated)
            {
                @Html.ActionLink("TimeEntry", "Index", "TimeEntry", null, new {@class = "navbar-brand"})
            }
            else
            {
                @Html.ActionLink("TimeEntry", "Login", "User", null, new {@class = "navbar-brand"})
            }

        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Clock In/Out(WIP)", "Index", "TimeEntry")</li>
                <li>@Html.ActionLink("Time Summary", "BackEnd", "TimeEntry")</li>



                <li>@Html.ActionLink("Users", "Index", "User")</li>
                <li><a href="#search">Search</a></li>


            </ul>

            <ul class="nav navbar-nav navbar-right">
                @if (Request.IsAuthenticated)
                {
                    <li> @Html.ActionLink("Log Out", "Logout", "User")</li>

                }
            </ul>


        </div>
    </div>
</div>

<div id="search">
    <button type="button" class="close" style="padding-top: 35px">x</button>
    @using (Html.BeginForm("Search", "User", FormMethod.Get))
    {
            @Html.TextBox("searchString","", new {@placeholder = "Search by First Name"})
            <button type="button" class="btn btn-primary">Search Rando</button>   
    }
</div>

<div class="container body-content">
    @RenderBody()
    <hr/>
    <footer>
        <p>&copy; @DateTime.Now.Year - My TimeClock Application</p>
    </footer>
</div>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>

This line is cancelling the default event action. 此行正在取消默认事件操作。 Take it out to allow the form to submit. 取出以允许表单提交。

event.preventDefault();

This will allow the form to submit. 这将允许表单提交。 Although the other two lines in that function will no longer serve any purpose, since they are modifying the state of the document, but the browser will navigate away from the document as soon as the form submission happens. 尽管该函数中的其他两行将不再具有任何作用,因为它们正在修改文档的状态,但是一旦提交表单,浏览器就会离开文档。

<button type="button" class="btn btn-primary">Search Rando</button>

should be changed to 应该更改为

<button type="submit" class="btn btn-primary">Search Rando</button>

by altering the type to submit 通过更改submit type

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

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