Html 如何在asp.net mvc中向布局视图添加搜索栏?
在默认的ASP.NET MVC应用程序中,我试图添加一个搜索框(它有一个提交按钮,将重定向到搜索控制器的索引操作)。现在我遇到的问题是在这种情况下html/css的工作方式。在这里看一下我的视图代码Html 如何在asp.net mvc中向布局视图添加搜索栏?,html,css,asp.net-mvc,asp.net-mvc-4,razor,Html,Css,Asp.net Mvc,Asp.net Mvc 4,Razor,在默认的ASP.NET MVC应用程序中,我试图添加一个搜索框(它有一个提交按钮,将重定向到搜索控制器的索引操作)。现在我遇到的问题是在这种情况下html/css的工作方式。在这里看一下我的视图代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-s
<!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>© @DateTime.Now.Year - Test</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
</html>
@标题-测试
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/modernizer”)
@ActionLink(“Test”、“Index”、“Home”、new{area=”“}、new{@class=“navbar brand”})
@使用(Html.BeginForm(“Index”,“Search”,FormMethod.Get))
{
- @Html.TextBox(“id”,ViewBag.CurrentFilter为字符串)
}
@Html.Partial(“_LoginPartial”)
@RenderBody()
&抄袭@DateTime.Now.Year-测试
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)
@RenderSection(“脚本”,必需:false)
现在,这是我的浏览器中的视图。
现在我只想让搜索框和搜索按钮水平对齐,就像我的“测试”字一样,我也不想让黑色导航栏变大(在我们打开默认mvc应用程序时得到的默认代码中,这个导航栏的大小更小)。那么如何修复此html呢?对于保存搜索控件的
,您应该使用导航栏左侧
,而不是使用导航栏折叠
因此,改变这一点:
<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>
@使用(Html.BeginForm(“Index”,“Search”,FormMethod.Get))
{
- @Html.TextBox(“id”,ViewBag.CurrentFilter为字符串)
}
为此:
<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>
@使用(Html.BeginForm(“Index”,“Search”,FormMethod.Get))
{
- @Html.TextBox(“id”,ViewBag.CurrentFilter为字符串)
}
输出:
对于保存搜索控件的
,您应该使用导航栏左侧
,而不是使用导航栏折叠
因此,改变这一点:
<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>
@使用(Html.BeginForm(“Index”,“Search”,FormMethod.Get))
{
- @Html.TextBox(“id”,ViewBag.CurrentFilter为字符串)
}
为此:
<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>
@使用(Html.BeginForm(“Index”,“Search”,FormMethod.Get))
{
- @Html.TextBox(“id”,ViewBag.CurrentFilter为字符串)
}
输出: