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.
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?)
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.
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:
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
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
Hope this helps,
/Dennis
I did.. I got the following error on a YSOD. :/
Hi Tom,
Okay what if you do something like this.
Hope this works for you.
/Dennis
That one throws a different error:
Hi Tom,
Okay I think I found a way that you could do it.
Hope this helps,
/Dennis
This time it's a different one:
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
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
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
is working on a reply...