如何在_Layout.cshtml中调用搜索方法?
How to call a search method in a _Layout.cshtml?
我正在尝试创建一个在我的 _Layout.cshtml 中调用的搜索方法,当我点击提交按钮时,用户被重定向到 "search view"。
我关注此 tutorial,但作者在索引视图调用了您的方法,我需要在 _Layout 调用该方法以重定向到另一个视图,该视图将给我 return方法。
这是我的 _Layout,是来自 bootstrap 的基本导航栏,带有文本字段和按钮,用户可以放置他需要查找的内容:
@model IEnumerable<TestTcc2.Models.Musica>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/Bootstrap")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
</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>
<a class="navbar-brand" href="#">@Html.ActionLink("your logo here", "IndexOuvinte", "Home")</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "IndexOuvinte", "Home")</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Manage Account", "Manage", "Account")</li>
<li>@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>}</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
<div class="container">
@RenderSection("featured", required: false)
@RenderBody()
</div>
<!--Fim container -->
</body>
</html>
这是我想在 _Layout.cshtml:
调用的方法
public ActionResult Search(string search)
{
var musicas = from m in db.Musicas select m;
if (!String.IsNullOrEmpty(search))
{
musicas = musicas.Where(s => s.Nome.Contains(search));
return RedirectToAction("Search"); //name of view that will return the data
}
return View(musicas);
}
好吧,您可以考虑为表单指定一个 action
或者只使用适当的助手来生成它:
@using (Html.BeginForm("Search", "SomeController", null, FormMethod.Post, new { @class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
}
我正在尝试创建一个在我的 _Layout.cshtml 中调用的搜索方法,当我点击提交按钮时,用户被重定向到 "search view"。
我关注此 tutorial,但作者在索引视图调用了您的方法,我需要在 _Layout 调用该方法以重定向到另一个视图,该视图将给我 return方法。
这是我的 _Layout,是来自 bootstrap 的基本导航栏,带有文本字段和按钮,用户可以放置他需要查找的内容:
@model IEnumerable<TestTcc2.Models.Musica>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/Bootstrap")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
</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>
<a class="navbar-brand" href="#">@Html.ActionLink("your logo here", "IndexOuvinte", "Home")</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "IndexOuvinte", "Home")</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Manage Account", "Manage", "Account")</li>
<li>@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>}</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
<div class="container">
@RenderSection("featured", required: false)
@RenderBody()
</div>
<!--Fim container -->
</body>
</html>
这是我想在 _Layout.cshtml:
调用的方法public ActionResult Search(string search)
{
var musicas = from m in db.Musicas select m;
if (!String.IsNullOrEmpty(search))
{
musicas = musicas.Where(s => s.Nome.Contains(search));
return RedirectToAction("Search"); //name of view that will return the data
}
return View(musicas);
}
好吧,您可以考虑为表单指定一个 action
或者只使用适当的助手来生成它:
@using (Html.BeginForm("Search", "SomeController", null, FormMethod.Post, new { @class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
}