Copied to clipboard

Flag this post as spam?

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


  • Sarah Marriott 2 posts 82 karma points
    Dec 13, 2018 @ 16:48
    Sarah Marriott
    0

    Noob Question - Partial View Macros

    Hi,

    I am just learning Umbraco, and have been following the tutorials. I have setup my own document types, but essentially replaced the word 'Article' for 'Lesson' for my own purposes.

    I am encountering an error when following this article

    https://our.umbraco.com/documentation/Tutorials/Creating-Basic-Site/Articles-Parent-and-Article-Items

    This is specifically in the razor code for the Partial view Macro File

    The error is:

    'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'lessonContents'

    my code is:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    @{ var selection = Model.Content.Site().FirstChild("lessonsMain").Children("lesson") .Where(x => x.IsVisible()) .OrderBy("CreateDate desc"); }

    @foreach(var item in selection){
        <div class="article">
        <div class="articletitle"><a href="@item.Url">@item.Name</a></div>
        <div class="articlepreview">@Umbraco.Truncate(@item.lessonContents,100) <a href="@item.Url">Read More..</a></div>
    </div>
    <hr/>
    }
    

    I have a property in my Lesson document type named accordingly: enter image description here

    Can anyone point out where i may be going wrong?

    TIA

    Sarah

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Dec 14, 2018 @ 00:37
    Alex Skrypnyk
    100

    Hi Sarah

    Welcome to our forum!!!

    enter image description here

    Try this code:

    <div class="articlepreview">@Umbraco.Truncate(@item.GetPropertyValue<string>("lessonContents"),100) <a href="@item.Url">Read More..</a></div>
    

    "item" is a PublishedContent class, you can't access property values just by property alias.

    You have to use strongly typed models and models builder, read more - https://our.umbraco.com/documentation/reference/templating/modelsbuilder/

    Or just use as I showed you "item" and GetPropertyValue("propertyAlias") method. Read more about Umbraco helper methods - https://our.umbraco.com/documentation/reference/querying/umbracohelper/

    Post as many questions as you can and we will do great progress in Umbraco learning together with the community!

    Thanks,

    Alex

  • Sarah Marriott 2 posts 82 karma points
    Dec 14, 2018 @ 08:58
    Sarah Marriott
    0

    Thank you Alex,

    I've just tried your code and it works perfectly!

    I'll read the documentation links you've given me - I am really keen to learn Umbraco as I think it could be a fantastic tool.

    Oh, and I will definitely be back here when I get stuck again ;-)

    Thanks again,

    Sarah

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies