问题描述
我想在版面页面中创建一个搜索文本字段,以便可以在整个网站上进行搜索。问题在于它在控制器中工作良好,但在布局上工作时却不起作用(可能是路由选择不正确,因为它会在任何子页面上添加“?搜索”并且不会重定向至正确的控制器和操作)。
_Layout.cshtml
@using (Html.BeginForm("Search","Home",FormMethod.Get ))
{
@Html.TextBox("searching")
<input type="submit" value="Search" placeholder = "Search" />
}
HomeController.cshtml
public ActionResult Search(string searching)
{
IEnumerable<Book> books = from t in Book.allBooks select t;
if (!String.IsNullOrEmpty(searching))
{
books = books.Where(a => a.Title.ToLower().Contains(searching.ToLower()));
}
return View(books.ToList());
}
查看= Search.cshtml
@model IEnumerable<LibraryMVC.Core.Models.Book>
@{
ViewBag.Title = "Search";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2 style="margin-top: 30px">Results for: </h2>
<hr style="max-width:500px;" />
<div class="container" style="padding-bottom:60px; display: flex; margin: 0px; font-size: 20px; padding-top: 10px; max-width:100%;">
<div class="row" style="width: 100%; justify-content: center; margin-left: 0px">
@using (Html.BeginForm("Search",FormMethod.Get))
{
@Html.TextBox("searching")
<input type="submit" value="Search" />
}
@if (Model.Count() == 0)
{
<h2 style="margin-top: 30px">Book have not been found</h2>
}
else
{
foreach (var item in Model)
{
<div class="col-12 col-md-6 col-lg-6 col-xl-3 align-items-center" style="margin-top: 10px; margin-bottom: 10px;">
<div class="col-md-12 d-flex justify-content-center">
<img src="~/Content/BookImages/@item.Image" class="img-thumbnail" style="height: 400px; width: 250px;" />
</div>
<div class="col-md-12 text-center">
<strong>@Html.ActionLink(item.Title,"Details",new { id = item.Id },null)</strong>
</div>
<div class="col-md-12 text-center">
@item.WriterFirstName
</div>
<div class="col-md-12 text-center">
@item.WriterLastName
</div>
</div>
}
}
</div>
</div>
请帮助。谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)