[英]How can I add result from searching to title in 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>
搜索.cshtml:
@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
{
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", "Home", 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", "Home", 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 的壞處是,如果有人更改了控制器中的代碼,那么您只能在運行時發現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.