Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt 358 posts 841 karma points
    Sep 24, 2019 @ 07:26
    Matt
    0

    Search form on different page then the results?

    Hello,

    I have the follow search form which works fine, however to search I have to click on the search page which has the code below in;

    What I'm looking to do is have the search box on the home page, which once search then gets redirect to /search to display the results? how could I tweak the below code to achieve this?

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Search>
    @using ContentModels = Umbraco.Web.PublishedModels;
    @using Umbraco.Examine
    @{
        Layout = "master.cshtml";
        var searchQuery = Request.QueryString["q"];
    }
    
    <div class="light-section">
        <div class="w-container">
    
           <div class="contact-clean">
        <form action="@Model.Url" method="GET" id="search">
            <div class="form-group">
                <div class="row">
                    <div class="col-xs-12 col-sm-8">
                        <input type="text" class="form-control col-xs-6" placeholder="Search..." name="q" value="@searchQuery" />
                    </div>
                    <div class="col-xs-12 col-sm-4">
                        <button class="btn btn-primary">Search <i class="fa fa-search"></i></button>
                    </div>
                    <div class="col-xs-12">
                        @if (!string.IsNullOrWhiteSpace(searchQuery))
                        {
                            var results = Umbraco.ContentQuery.Search(searchQuery, string.Empty);
                            long resultCount = results != null && results.Any() ? results.Count() : 0;
                            <div class="searchresults">
                                <p>We found <strong>@resultCount</strong> result@(resultCount != 1 ? "s" : "") when searching for <strong>@searchQuery</strong></p>
                                @if (resultCount > 0)
                                {
                                    <ul>
                                        @foreach (var result in results)
                                        {
                                            <li>
                                                <h3><a href="@result.Content.Url">@result.Content.Name</a></h3>
                                                <p>@(result.Content.Value<string>("metaDescription"))</p>
                                            </li>
                                        }
                                    </ul>
                                }
                            </div>
                        }
                    </div>
                </div>
            </div>
        </form>
    </div>
        </div>
    </div>
    

    Thanks

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 24, 2019 @ 07:56
    Shaishav Karnani from digitallymedia.com
    101

    Hi Matt,

    You can simply add below code on home page. This will redirect user to search page with query string.

    You can also make "/search" dynamic and not static as below. On Home page, you can create a Content Picker named Search page and replace the url of the Content Picker with "/search".

    <form action="/search" method="GET" id="search">
            <div class="form-group">
                <div class="row">
                    <div class="col-xs-12 col-sm-8">
                        <input type="text" class="form-control col-xs-6" placeholder="Search..." name="q" value="@searchQuery" />
                    </div>
                    <div class="col-xs-12 col-sm-4">
                        <button class="btn btn-primary">Search <i class="fa fa-search"></i></button>
                    </div>
    </div>
    </form>
    

    Hope it helps to solve the issue.

    Cheers,

    Shaishav

  • Matt 358 posts 841 karma points
    Sep 24, 2019 @ 09:24
    Matt
    0

    Hello Shaishav,

    Thanks that worked! :)

Please Sign in or register to post replies

Write your reply to:

Draft