Copied to clipboard

Flag this post as spam?

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


  • Sean 141 posts 179 karma points
    Jul 04, 2014 @ 00:15
    Sean
    0

    Umbraco.TypedSearch handle empty resultset

    Hi There,

    I'm using the Examine search on my Umbraco 7.1.4 site. The search is working fiine and is returning results. I'm wanting to know how I can display a message when there are no results in the resultset.

    Apologies for the silly question but I thought it would just be a case of checking for type or length, so far everything I have tried has not worked.

     

    foreach (var result in Umbraco.TypedSearch(Request.QueryString["q"]))

            {

               if (result.GetType().ToString() == " ")

               {

                     <section class="section">

                       <div class="row col-span-12">

                        <h3>No results found</h3>

                       </div>

                     </section>

                   return;

     

               }

    }

  • Ismael 71 posts 354 karma points
    Jul 04, 2014 @ 01:31
    Ismael
    101

    Hi 

    I think if you store the search results in a variable then you can check that before you try iterate or output your 'no results' if it's empty. As you have it, it will not enter the foreach if there are no results.

    for example:

    var results = Umbraco.TypedSearch(Request.QueryString["q"]))
    if( results.Any() ){
          foreach(var result in results){
    <a href="@result.Url">@result.Name</a>           
          }
    }else{
    No results found
    }

     

    cheers

     

  • Sean 141 posts 179 karma points
    Jul 04, 2014 @ 02:36
    Sean
    0

    thank you ! Works great.

Please Sign in or register to post replies

Write your reply to:

Draft