Copied to clipboard

Flag this post as spam?

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


  • Kevin John 8 posts 28 karma points
    Oct 09, 2012 @ 16:54
    Kevin John
    0

    Razor macro - how do I display a property alias in a list?

    Hi.  New to Umbraco, and Razor, and have read lots of docs and watched videos. I am trying to display a list of items in another page.  I am writing my first Razor scripted macro, which I want to insert in a template for the new page.

    This is what I have and this displays the name, and links to the item page.  This works OK:

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @foreach (var item in Model.Children.Where("umbracoNaviHide != true").OrderBy("UpdateDate"))
    {
        <h3>
            <a href="@item.Url">@item.Name</a>
        </h3>
      
    }

    On my top level item doc. type I have a property alias called "widgetmodelDescription" - I would like to display this description under the 'item.name' above.  I have tried several things but failed.  Can anyone help?

     

    In addition,  under each of these top level items, I also want to show the names and descriptions of their child items, that are associated with each top level item. Example:

    Item 1 'name' + 'description'

    • Item 1 child 1 'name' + 'description'
    • Item 1 child 2 'name' + 'description'
    • Item 1 child 3 'name' + 'description'

    Item 2 'name' + 'description'

    • Item 2 child 1 'name' + 'description'
    • Item 2 child 2 'name' + 'description'

     

    Thanks in advance.

     

     

  • Funka! 398 posts 660 karma points
    Oct 10, 2012 @ 01:07
    Funka!
    0

    Hi Kevin,

    To display a property value in Razor, the Model or item is a dynamic object that lets you just read it like a regular property.

    For example, you could try the following:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @foreach (var item in Model.Children.Where("umbracoNaviHide != true").OrderBy("UpdateDate"))
    {
        <h3>
            <a href="@item.Url">@item.Name</a>
        </h3>
        <div>
            @item.widgetmodelDescription
        </div>
        <ul>
            @foreach (var child in item.childWidgetDocumentTypeAlias)
            {
                <li>@child.Name - @child.widgetmodelDescription</li>
            }
        </ul>
    }

    Notice that you have to fill in your own "childWidgetDocumentTypeAlias" since I don't know what that is. Also be sure to be very careful about the casing: for example, widgetmodelDescription and widgetModelDescription are NOT the same thing!

    Best of luck to you!

  • Kevin John 8 posts 28 karma points
    Oct 12, 2012 @ 17:37
    Kevin John
    0

    Thanks Funka. I will try that :)

  • 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