1

I wanted to make a search text field in my layout page so searching would be available all over the website. The issue is that it works well in controller but does not work while working on layout (probably routing is bad because it adds "?searching" to any subpage and does not redirect to correct controller and action).

_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());
        }

View = 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", "Home", 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", "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>
            }
        }
    </div>
</div>

Please help. Thank you

  • If you change "FormMethod.Get" to "FormMethod.Post" will solve your "?searching" issue. Also i added some other controllers and views under the same _Layout page all page showing the search box and it works perfectly. – Biju Kalanjoor Aug 25 '20 at 14:46
  • @derloopkat it works. The issue was
    tag. I have removed it from my navbar and placed
      and
    • and it works well. At the moment my issue is that after each search my previous one "stays". So first time when I search I see only one book with this title but after every time it multiplies. How can I deal with it?
    – Rafał Pawłowski Aug 26 '20 at 10:38
  • @RafałPawłowski Have you tried to [remove the parameter](https://stackoverflow.com/questions/21280071/clear-or-remove-query-string-in-asp-net) from the URL? – derloopkat Aug 26 '20 at 12:47

0 Answers0