Copied to clipboard

Flag this post as spam?

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


  • pranjal 75 posts 188 karma points
    May 11, 2018 @ 07:47
    pranjal
    0

    if no data found in Umbraco Search i need to display error message how to do that

    My code is

    @{
        var searchQuery = Request.QueryString["query"];
    
        if (!string.IsNullOrEmpty(searchQuery))    
        {
            <div class="searchresults">
                <p>Your search results for <strong>@searchQuery</strong></p>
                <ul>
                    @foreach (var result in Umbraco.Search(searchQuery))
                    {
                        <li>
                            <a href="@result.Url">@result.Name</a>
                        </li>
                    }
                </ul>
            </div>
         if (Umbraco.Search(searchQuery) == null) {
             <li>No result found</li>
         }
     }
    }
    

    any solution?

  • Neil Hodges 338 posts 987 karma points
    May 11, 2018 @ 08:57
    Neil Hodges
    100

    You could do something like this?

        @{
        var searchQuery = Request.QueryString["query"];
    
        if (!string.IsNullOrEmpty(searchQuery))
        {
            var searchResults = Umbraco.Search(searchQuery);
            if (searchResults != null)
            {
                <div class="searchresults">
                    <p>Your search results for <strong>@searchQuery</strong></p>
                    <ul>
                        @foreach (var result in searchResults)
                        {
                            <li>
                                <a href="@result.Url">@result.Name</a>
                            </li>
                        }
                    </ul>
                </div>
            }
            else
            {
                <div class="searchresults">
                    <p>Your search results for <strong>@searchQuery</strong></p>
                    <ul>
                        <li>No result found were found for your search quesry: @searchQuery<</li>
                    </ul>
                </div>
            }
        }
    }
    
  • pranjal 75 posts 188 karma points
    May 11, 2018 @ 10:18
    pranjal
    0

    still have problem

    if user enter junk data then it doesn't execute else command it goes to if part

  • Neil Hodges 338 posts 987 karma points
    May 11, 2018 @ 10:25
    Neil Hodges
    0

    Have you put a break point on

    searchResults
    

    To see whats returning? You can then filter on the results.

  • pranjal 75 posts 188 karma points
    May 11, 2018 @ 11:41
    pranjal
    1

    Worked

    with null i just put searchResults.Any() and code goes to Else condition on junk data

    Final Working Code

    var searchResults = Umbraco.Search(searchQuery);
    if (!string.IsNullOrEmpty(searchResults)  &&  searchResults.Any())
    {
        <div class="searchresults">
            <div class="container">
                @if (searchFlag)
                {
    
                    <p>Ministry of Interior</p>
                }
                else
                {
                    <p>Your search results for <strong>@searchQuery</strong></p>
    
                }
                <ul>
    
                    @foreach (var result in searchResults)
                    {
                        <li>
                            <a href="@result.Url">@result.Name</a>
                        </li>
                    }
                </ul>
               @*<div class="no-result">No search result found...</div>*@
    
    
            </div>
        </div>
    }
    else {
    
        <li>NO Search Result</li>
    }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft