0

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. 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>
7
  • Your cancelling the submit event (twice!) - event.preventDefault(); and return false; Commented Feb 9, 2015 at 1:53
  • If you want to submit the form, don't cancel it! (remove the last block of code starting with $('form').submit(function (event) {. But you have not shown your view so its difficult to understand what your really trying to do here. Commented Feb 9, 2015 at 1:58
  • @StephenMuecke : i will post the view. Commented Feb 9, 2015 at 1:59
  • I don't understand the issue. What do you mean by the submission works, but not when it is used in the razor file? Are you wanting to update the current page based on the value of searchString? (rather than post and redirect) - in which case you would use ajax Commented Feb 9, 2015 at 2:08
  • When i use the javascript code, i can log in, i.e. clicking on the log in button does nothing, but when i remove this script it works. Commented Feb 9, 2015 at 2:09

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
<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

Comments

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.