C# 如何将搜索结果添加到MVC5中的标题?

C# 如何将搜索结果添加到MVC5中的标题?,c#,asp.net-mvc,search,C#,Asp.net Mvc,Search,我想在我的子页面的标题中添加搜索查询名称,例如,如果有人在搜索框中键入John,他/她收到的页面是:搜索结果:John 家庭控制器: public ActionResult Search(string searching) { IEnumerable<Book> books = from t in Book.allBooks select t; if (!String.IsNullOrEmpty(searching))

我想在我的子页面的标题中添加搜索查询名称,例如,如果有人在搜索框中键入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());
        }
公共操作结果搜索(字符串搜索)
{
IEnumerable books=从书本中的t开始。所有书本选择t;
如果(!String.IsNullOrEmpty(正在搜索))
{
books=books.Where(a=>a.Title.ToLower().Contains(search.ToLower());
}
返回视图(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>
  • @使用(Html.BeginForm(“搜索”、“主页”、FormMethod.Get)) { @文本框(“搜索”) }
  • Search.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>
                }
            }
    
    @使用(Html.BeginForm(“搜索”、“主页”、FormMethod.Get))
    {
    @*@文本框(“搜索”)
    *@
    }
    @if(Model.Count()==0)
    {
    找不到任何同名的书
    }
    其他的
    {
    foreach(模型中的var项目)
    {
    @Html.ActionLink(item.Title,“详细信息”,“主页”,新的{id=item.id},空)
    @item.WriterFirstName
    @item.WriterLastName
    }
    }
    

    感谢您的帮助

    这可能对您有用:

    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());
            }
    
    公共操作结果搜索(字符串搜索)
    {
    IEnumerable books=从书本中的t开始。所有书本选择t;
    如果(!String.IsNullOrEmpty(正在搜索))
    {
    books=books.Where(a=>a.Title.ToLower().Contains(search.ToLower());
    }
    ViewBag.SearchTerm=搜索;
    返回视图(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>
                }
            }
    
    @使用(Html.BeginForm(“搜索”、“主页”、FormMethod.Get))
    {
    @*@文本框(“搜索”)
    *@
    }
    @if(Model.Count()==0)
    {
    找不到任何同名的书
    }
    其他的
    {
    搜索结果:@ViewBag.SearchTerm
    foreach(模型中的var项目)
    {
    @Html.ActionLink(item.Title,“详细信息”,“主页”,新的{id=item.id},空)
    @item.WriterFirstName
    @item.WriterLastName
    }
    }
    
    请注意,这是一个快速而肮脏的解决方案。正确的方法是创建一个ViewModel并添加图书和搜索词

    public class SearchBookViewModel {
        public IEnumerable<Book> Books {get; set;}
        public string SearchTerm {get; set;}
    }
    
    公共类SearchBookViewModel{
    公共IEnumerable图书{get;set;}
    公共字符串搜索项{get;set;}
    }
    

    如果无法更改ViewModel,请使用ViewBag。ViewBag的坏处在于,如果有人更改了控制器中的代码,那么您只能在运行时才能找到它。

    这可能适用于您:

    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());
            }
    
    公共操作结果搜索(字符串搜索)
    {
    IEnumerable books=从书本中的t开始。所有书本选择t;
    如果(!String.IsNullOrEmpty(正在搜索))
    {
    books=books.Where(a=>a.Title.ToLower().Contains(search.ToLower());
    }
    ViewBag.SearchTerm=搜索;
    返回视图(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>
                }
            }
    
    @使用(Html.BeginForm(“搜索”、“主页”、FormMethod.Get))
    {
    @*@文本框(“搜索”)
    *@
    }
    @if(Model.Count()==0)
    {
    找不到任何同名的书
    }
    其他的
    {
    搜索结果:@ViewBag.SearchTerm
    foreach(模型中的var项目)
    {
    @Html.ActionLink(item.Title,“详细信息”,“主页”,新的{id=item.id},空)
    @item.WriterFirstName
    @item.WriterLastName
    }
    }
    
    请注意,这是一个快速而肮脏的解决方案。正确的方法是创建一个ViewModel并添加图书和搜索词

    public class SearchBookViewModel {
        public IEnumerable<Book> Books {get; set;}
        public string SearchTerm {get; set;}
    }
    
    公共类SearchBookViewModel{
    公共IEnumerable图书{get;set;}
    公共字符串搜索项{get;set;}
    }
    

    如果无法更改ViewModel,请使用ViewBag。ViewBag的坏处在于,如果有人更改了控制器中的代码,那么您只能在运行时才能发现。

    您可以创建一个ViewModel,其中的列表和标题作为属性,或者将标题放在您要查找的ViewBag中。您可以创建一个ViewModel,其中的列表和标题作为属性,或者将标题放在您要查找的ViewBag中。