如何解决如何在MVC5中将搜索结果添加到标题?
我想在子页面的页眉中添加搜索查询名称,例如,如果有人在搜索框中键入John,则他/她收到的页面是:搜索结果:John
家庭控制器:
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());
}
_Layout.cshtml:
<li class="d-none d-lg-block justify-content-center" style="align-self: center; padding-right: 10px">
@using (Html.BeginForm("Search","Home",FormMethod.Get))
{
<i class="fas fa-search" aria-hidden="true" style="padding-right: 5px"> </i>
@Html.TextBox("searching")
<input type="submit" value="Search" />
}
</li>
Search.cshtml:
@using (Html.BeginForm("Search",FormMethod.Get))
{
@*@Html.TextBox("searching")
<input type="submit" value="Search" />*@
}
@if (Model.Count() == 0)
{
<h2 style="margin-top: 30px">Not found any book with this name</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>
}
}
感谢您的帮助
解决方法
这可能对您有用:
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()));
}
ViewBag.SearchTerm = searching;
return View(books.ToList());
}
在您看来:
@using (Html.BeginForm("Search","Home",FormMethod.Get))
{
@*@Html.TextBox("searching")
<input type="submit" value="Search" />*@
}
@if (Model.Count() == 0)
{
<h2 style="margin-top: 30px">Not found any book with this name</h2>
}
else
{
<h2 style="margin-top: 30px">Search results for: @ViewBag.SearchTerm</h2>
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>
}
}
请记住,这是一种快速而肮脏的解决方案。合适的方法是创建一个ViewModel并添加书籍和搜索词。
public class SearchBookViewModel {
public IEnumerable<Book> Books {get; set;}
public string SearchTerm {get; set;}
}
如果您不能更改ViewModel,请使用ViewBag。 ViewBag的坏处在于,如果有人更改了控制器中的代码,那么您只会在运行时发现它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。