Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 342 karma points
    Sep 09, 2016 @ 10:52
    Tom Cowling
    0

    Creating search results partial view

    Hi,

    I'm trying to work out the correct syntax for one part of a for loop and just can't get it right. I've been trying a few different things and just can't quite get it right. I'm trying to grab the first 150 characters of the html in a grid and just can't quite do it.

    Currently I can get the grid data thrown in, but can't work out what to do next.

    Here's my code:

    int count = 0;
    
    <div class="list-group">
        @foreach (var result in Umbraco.TypedSearch(Request.QueryString["q"]).Where("hideFromSearch == False"))
        {
            count++;
    <a href="@result.Url" class="list-group-item list-group-item-action">
                <h4 class="list-group-item-heading">@result.Name</h4>
                <p class="list-group-item-text">@result.GetPropertyValue("mainContent") <br/>
                    <span class="text-muted">@result.Url</span></p>
        </a>
        }
    

    The problem area is: @result.GetPropertyValue("mainContent")

    I've tried adding a .substring, but it throws an error. For this I used ..mainContent").ToString().Substring(0,150)

    I also tried GetGridHtml("mainContent") instead of GetPropertyValue, but this also threw an error.

    Any guidance would be much appreciated!

    Thanks,

    Tom

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Sep 09, 2016 @ 12:19
    Dennis Aaen
    0

    Hi Tom,

    Have you tried to use the Umbraco helper method, called @Umbraco.Truncate()

    https://our.umbraco.org/documentation/reference/querying/umbracohelper/#truncate-string-html-int-length-bool-addellipsis

    @Umbraco.Truncate(result.mainContent, 150)
    

    Hope this helps,

    /Dennis

  • Tom Cowling 144 posts 342 karma points
    Sep 09, 2016 @ 12:29
    Tom Cowling
    0

    I did.. I got the following error on a YSOD. :/

    CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'mainContent' and no extension method 'mainContent' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Sep 09, 2016 @ 12:33
    Dennis Aaen
    0

    Hi Tom,

    Okay what if you do something like this.

    @Umbraco.Truncate(result.GetPropertyValue("mainContent"), 150)
    

    Hope this works for you.

    /Dennis

  • Tom Cowling 144 posts 342 karma points
    Sep 09, 2016 @ 12:35
    Tom Cowling
    0

    That one throws a different error:

    CS1502: The best overloaded method match for 'Umbraco.Web.UmbracoHelper.Truncate(System.Web.IHtmlString, int)' has some invalid arguments
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Sep 09, 2016 @ 12:45
    Dennis Aaen
    0

    Hi Tom,

    Okay I think I found a way that you could do it.

    @Umbraco.Truncate(result.GetPropertyValue("mainContent").ToString(), 150, false)
    

    Hope this helps,

    /Dennis

  • Tom Cowling 144 posts 342 karma points
    Sep 09, 2016 @ 12:48
    Tom Cowling
    0

    This time it's a different one:

    Object reference not set to an instance of an object.
    System.NullReferenceException: Object reference not set to an instance of an object.
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Sep 09, 2016 @ 12:54
    Dennis Aaen
    100

    Hi Tom,

    Could you check that this property mainContent has some content from the page that you are view in the browser or perhaps build an if statement in to ensure the field is not empty.

    /Dennis

  • Tom Cowling 144 posts 342 karma points
    Sep 09, 2016 @ 13:06
    Tom Cowling
    0

    Horray! Very nearly there. :D

    (I popped in an if statement - I completely forgot the home page didn't have a mainContents grid. Sorry about that.

    Is there any way I can couple this with GetGridHtml to render the html from it?

    Thanks a lot for your help with this,

    Tom

  • Tom Cowling 144 posts 342 karma points
    Sep 09, 2016 @ 13:32
    Tom Cowling
    0

    Sorted it!

    @Umbraco.Truncate(@result.GetGridHtml("mainContent").ToString(), 200)

    Via this link: https://our.umbraco.org/forum/using/ui-questions/65276-Automaticaly-show-short-version-of-a-range-of-pages-on-one-overviewpage#comment-220777

    Thanks again for pointing me in the right direction Dennis

Please Sign in or register to post replies

Write your reply to:

Draft