【问题标题】:How can I add result from searching to title in MVC5?如何将搜索结果添加到 MVC5 中的标题?
【发布时间】:2020-12-15 04:15:47
【问题描述】:

我想在子页面的标题中添加搜索查询名称,例如,如果有人在搜索框中输入 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>
            }
        }

感谢您的帮助

【问题讨论】:

  • 您可以创建一个 ViewModel,其中包含列表和标题作为属性,或者将标题放在 ViewBag 中
  • 也许你正在寻找this

标签: c# asp.net-mvc search


【解决方案1】:

这可能对你有用:

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 的坏处是,如果有人更改了控制器中的代码,那么您只会在运行时发现。

【讨论】:

    猜你喜欢
    • 2014-08-03
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    相关资源
    最近更新 更多