Copied to clipboard

Flag this post as spam?

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


  • Leopold Kristjansson 3 posts 104 karma points
    Aug 17, 2016 @ 22:54
    Leopold Kristjansson
    1

    Custom menu - access custom properties for menu items

    I am trying to build a menu in Umbraco where the user can mark pages/menu-items with a different color.

    I have a custom property (checkbox) called "MarkAsRed" on the document type I use for all the nodes on my page.

    The code below gives me an error on @page.MarkAsRed. However @home.MarkAsRed works and gives me a value for the root node.

    What is missing so I can get the correct value for each node in the menu?

    "Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'MarkAsRed' and no extension method 'MarkAsRed' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)"

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var selection = CurrentPage.Site().Children.Where("Visible");
        var home = CurrentPage.Site();
    }
    
    <div class="navigation">
        <ul class="lev-1">
            @foreach (var page in Model.Content.AncestorOrSelf(1).Children.Where("Visible"))
            {
                <li>
                    @{
                        if (page.Children.Where("Visible").Count() > 0)
                        {
                            <a data-mark-red="@page.MarkAsRed" href="@page.Url">@page.Name</a>
                            <ul class="lev-2">
                                @foreach (var subpage in page.Children.Where("Visible"))
                                {
                                    <li>
                                        <a href="@subpage.Url">@subpage.Name</a>
                                    </li>
                                    foreach (var childPage in subpage.Children.Where("Visible"))
                                    {
                                        <ul class="lev-3">
                                            <li>
                                                <a href="@childPage.Url">@childPage.Name</a>
                                            </li>
                                        </ul>
                                    }
                                }
                            </ul>
                        } else
                        {
                            <a href="@page.Url">@page.Name</a>
                        }
                    }
                </li>
            }
        </ul>
    </div>
    
  • Dennis Adolfi 1072 posts 6378 karma points MVP 2x c-trib
    Aug 18, 2016 @ 12:14
    Dennis Adolfi
    100

    Hi Leopold.

    The home node is accessed dynamically but your children are accessed strongly typed. That's why you can write @home.MarkAsRead and not @page.MarkAsRead.

    So for accessing custom properties, write the following instead: @page.GetPropertyValue("markAsRead").

    If you want to read more about strongly/dynamic models there is a great article by Dave Woestenborghs: http://24days.in/umbraco/2015/strongly-typed-vs-dynamic-content-access/

    Have a good day!

  • 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