Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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;
@foreach (var result in Umbraco.TypedSearch(Request.QueryString["q"]).Where("hideFromSearch == False"))
<a href="@result.Url" class="list-group-item list-group-item-action">
<p class="list-group-item-text">@result.GetPropertyValue("mainContent") <br/>
The problem area is:
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!
Have you tried to use the Umbraco helper method, called @Umbraco.Truncate()
Hope this helps,
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?)
Okay what if you do something like this.
Hope this works for you.
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
Okay I think I found a way that you could do it.
@Umbraco.Truncate(result.GetPropertyValue("mainContent").ToString(), 150, false)
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.
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.
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,
Via this link:
Thanks again for pointing me in the right direction Dennis
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted