umbraco / UmbracoDocs

The official Umbraco Documentation
https://docs.umbraco.com
MIT License
266 stars 770 forks source link

Error in sample code #971

Closed telljimmy closed 5 years ago

telljimmy commented 6 years ago

The last part of this page shows sample for showing first 100 characters of an article (Fig 42). This is throwing an error saying @item.ArticleContents is not found.

This is the page with issues: https://github.com/umbraco/UmbracoDocs/edit/master/Tutorials/Creating-Basic-Site/Articles-Parent-and-Article-Items.md

dampee commented 6 years ago

Hi @telljimmy Are you sure you have created the Article Content property as mentioned in this image: afbeelding And can you make sure that you have the same field "alias" (the pad lock text).

telljimmy commented 6 years ago

Sorry for the late reply. I used Article Content instead of Article Contents. But its the same in both document type and in the Macro Partial. So hope it should have worked. images below. Thanks articleitemdoctype macropartial

telljimmy commented 6 years ago

the error is \Views\MacroPartials\listArticles.cshtml(23): error CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'ArticleContent' and no extension method 'ArticleContent' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)" string

dampee commented 6 years ago

To the team: It has to do with the modelsbuilder. We should take some time to validate whether that this tutorials has issues with the new modelsbuilder features.

Hi @telljimmy I am sure that if you do @(Umbraco.Truncate(item.GetPropertyValue<string>("articleContent"), 100) ... it will work.

andrewmumblebee commented 5 years ago

I also hit this issue when following the tutorial and using the sample code, using GetPropertyValue and casting to a string resolves this. I'm running v7.12.3

castlino commented 5 years ago

Also getting same error here.

Jeavon commented 5 years ago

I think you might be using the dynamic model (Model.Content) you will need to use the typed model (Model.TypedContent) for this extension method to work

castlino commented 5 years ago

@Jeavon yeah i think you are right, For anyone who's getting the same problem, try below

@Umbraco.Truncate(@Umbraco.Field(item, "articleContents"), 100)

jmayntzhusen commented 5 years ago

I have updated the sample to @(Umbraco.Truncate(item.GetPropertyValue<string>("articleContent"), 100)

Thanks for reporting the issue, will close it now 🙂