Copied to clipboard

Flag this post as spam?

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


  • Michael Beever 74 posts 155 karma points
    Jan 06, 2021 @ 14:54
    Michael Beever
    0

    Truncating words

    Hello,

    I am trying to create my own version of a Search on my website

    https://test.teespen.org.uk/search-results-2?query=form

    I am trying to give a brief description from the page but I cannot get it to truncate.

    This is the code so far:

      @{
                var searchTerm = string.Empty;
                searchTerm = string.IsNullOrEmpty(Request["query"])
                    ? string.Empty
                    : Request["query"];
    
                if (searchTerm == string.Empty)
                {
                    <p>Enter search term</p>
                }
                else
                {
                    if(ExamineManager.Instance.TryGetIndex("ExternalIndex", out var index))
                    {
                    var searcher = index.GetSearcher();
                    var textFields = new[] {"expandBox", "pageContent", "content", "newsType", "overview", "bannerText", "memberDetails", "termsOfReference", "area", "pageType", "expandBox", "formDescription"};
                    var results = searcher.CreateQuery("content").GroupedOr(textFields, searchTerm).Execute();
    
    
                        if (results.Any())
                        {
                            <ul style=" list-style-type:none;">
                                @foreach (var result in results)
                                {
                                    if (result.Id != null)
                                    {
                                        var node = Umbraco.Content(result.Id);
    
                                        @** Content **@
    
                                        var text = node.Value<string>("pageContent");
                                        var rText = Html.StripHtml(text, null);
    
    
                                        @** Overview **@
    
                                        var overview = node.Value<string>("overview");
                                        var rOverview = Html.StripHtml(overview, null);
    
    
                                        <li>
                                            <a href="@node.Url" style="color:blue; text-decoration:underline;">@node.Name</a>
                                            <span style="color:Green;">@node.Url</span>
    
                                            <p>@rText
    
                                            @rOverview</p>
    
                                        </li>
                                    }
                                }
                            </ul>
                        }
                        else
                        {
                            <p>No results found for query @searchTerm</p>
                        }
                    }
                    return;
                }
            }
    

    Everytime I use different truncate styles, it keeps coming back saying the value

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: html
    

    @Html.TruncateByWords(rrText, 10)

    I cannot figure it out any ideas please help.

  • Amir Khan 1287 posts 2744 karma points
    Jan 06, 2021 @ 15:50
    Amir Khan
    0

    You could try the Umbraco Helper: @Umbraco.Truncate(rText, 10)

    Also in your example you have a rrText in the truncate statement and rText in the main code block, possible its just a typo?

  • Michael Beever 74 posts 155 karma points
    Jan 07, 2021 @ 07:36
    Michael Beever
    0

    Hi,

    No rrText was not a typo, in another version I tried to convert rText to String which set to var rrText.

    I will try this method.

    Thanks

  • Michael Beever 74 posts 155 karma points
    Jan 07, 2021 @ 07:43
    Michael Beever
    0

    no that didn't work

  • Michael Beever 74 posts 155 karma points
    Jan 07, 2021 @ 08:41
    Michael Beever
    0

    Ok I have moved forward slightly.

    The problem seems to be how the string is been presented in the HTML truncate.

    var text = node.Value<string>("pageContent");
    var content = node.Value<string>("content");
    var rText = Html.StripHtml(text, null);
    var rContent = Html.StripHtml(content, null);
    string rrText = rText.ToHtmlString();
    

    Using this when the page loads I get an error.

    @Html.TruncateByWords(rrText, 10)
    

    However if I replace the rrText with a text statement it works.

    @Html.TruncateByWords("An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.", 10)
    
  • Michael Beever 74 posts 155 karma points
    Jan 07, 2021 @ 08:53
    Michael Beever
    0

    GOT IT

    Looked into @Html.Truncate and added all of the operators it required and it's now working.

    Thanks everyone

Please Sign in or register to post replies

Write your reply to:

Draft