代码之家  ›  专栏  ›  技术社区  ›  Sam Willis blabla test

Umbraco Razor导航访问自定义字段

  •  0
  • Sam Willis blabla test  · 技术社区  · 8 年前

    我正在尝试建立一个导航,根据CMS中的设置自动包含某些页面。

    我使用了Sitemap部分宏,并尝试添加检查页面属性的if语句,如下所示: @if (!item.HideSubPages) {

    但是,我只是得到以下错误:

    Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'HideSubPages' and no extension method 'HideSubPages' 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.Macros.PartialViewMacroPage
    @using Umbraco.Core.Models
    @using Umbraco.Web
    
    
    @*
        This snippet makes a list of links of all visible pages of the site, as nested unordered HTML lists.
    
        How it works:
        - It uses a custom Razor helper called Traverse() to select and display the markup and links.
    *@
    
    @{ var selection = Model.Content.Site(); }
    
    <div class="sitemap">
        @* Render the sitemap by passing the root node to the traverse helper, below *@
        @Traverse(selection)
    </div>
    
    
    @* Helper method to traverse through all descendants *@
    @helper Traverse(IPublishedContent node)
    {
        @* Update the level to reflect how deep you want the sitemap to go *@
        const int maxLevelForSitemap = 4;
    
        @* Select visible children *@
        var selection = node.Children.Where(x => x.IsVisible() && x.Level <= maxLevelForSitemap).ToArray();
    
        @* If any items are returned, render a list *@
        if (selection.Length > 0)
        {
            <ul>
                @foreach (var item in selection)
                {
                <li class="level-@item.Level">
                    <a href="@item.Url">@item.Name</a>
                    
                    @* Run the traverse helper again for any child pages *@
                    @if (!item.HideSubPages) {
                        @Traverse(item)
                    }
    
                </li>
                }
            </ul>
        }
    }
    1 回复  |  直到 8 年前
        1
  •  1
  •   Jannik Anker    8 年前

    尝试

    item.GetPropertyValue<bool>("hideSubPages") 
    

    相反?

    您需要一个动态类型,但正在处理ipublishedContent。动态并不是未来的证据,无论如何,因为对它们的支持将在下一个“大”版本的Umbraco(V8)结束,我相信。所以getproperty/getpropertyvalue是您的朋友:-)