我试图创建一个在我的_Layout.cshtml中调用的搜索方法,当我点击提交按钮时,用户被重定向到"搜索视图"。
我'在tutorial之后,作者在索引视图中调用你的方法,我需要调用_Layout上的方法重定向到另一个视图,它将返回方法。
这是我的_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);
}
答案 0 :(得分:2)
好吧,您可以考虑为表单指定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>
}