简体   繁体   中英

How to add a search bar to layout view in asp.net mvc?

In default ASP.NET MVC application, I am trying to add a search box(which has a submit button, that will redirect to my search controller's index action). Now the problem that I have is with the way html/css works in this case. Take a look at my view code here.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Test</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<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>
                @Html.ActionLink("Test", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                @using (Html.BeginForm("Index", "Search", FormMethod.Get))
            {
                    <ul class="nav navbar-nav">
                        <li>@Html.TextBox("id", ViewBag.CurrentFilter as string)</li>
                        <li><input type="submit" value="Search" /></li>
                    </ul>
                }
            </div>
            <div class="navbar-collapse collapse">
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - Test</p>
        </footer>
    </div>

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

Now here is the view for this in my browser. 在此处输入图片说明

Now I just want the search box and search button to be aligned horizontally as my "Test" word is and also I don't want that black navigation bar to become big(the size of this navigation bar is less in the default code that we get when we open the default mvc application) in doing so. So how do I fix this html?

Instead of using navbar-collapse collapse for the <div> which holds your search controls you should use navbar-left .

So change this:

<div class="navbar-collapse collapse">
    @using (Html.BeginForm("Index", "Search", FormMethod.Get))
    {
        <ul class="nav navbar-nav">
            <li>@Html.TextBox("id", ViewBag.CurrentFilter as string)</li>
            <li><input type="submit" value="Search" /></li>
        </ul>
    }
</div>

To this:

<div class="navbar-left" style="margin-top:8px;">
    @using (Html.BeginForm("Index", "Search", FormMethod.Get))
    {
        <ul class="nav navbar-nav">
            <li>@Html.TextBox("id", ViewBag.CurrentFilter as string)</li>
            <li><input type="submit" value="Search" /></li>
        </ul>
    }
</div>

Output:

引导导航栏中的对齐搜索框

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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