【问题标题】:Umbraco 6 razor menuUmbraco 6 剃须刀菜单
【发布时间】:2013-04-04 20:49:27
【问题描述】:

嗨,我想在我的 li 中添加一个“活动”类,如果它是活动的,或者我在它下面的页面上。我有以下代码

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
}

<nav class="topNav">
    <ul>
        @foreach (var item in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible() && x.IsDocumentType("Subfrontpage") || x.IsDocumentType("Procesguide")))
        {       
            <li>
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>
</nav>

我想我可以用这个做点什么,但它给出了一个错误

var isSelected = Model.Path.Contains(item.Id.ToString()) ? "active" : "";

            <li class="@Html.Raw(isSelected)">
                <a href="@item.Url">@item.Name</a>
            </li>

这是我得到的错误。第 10 行。

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

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

Source Error:


Line 8:         @foreach (var item in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible() && x.IsDocumentType("Subfrontpage") || x.IsDocumentType("Procesguide")))
Line 9:         {       
Line 10:            var isSelected = Model.Path.Contains(item.Id.ToString()) ? "active" : "";
Line 11: 
Line 12: <li class="@Html.Raw(isSelected)">

我现在已经尝试过了,但没有 lunk

<ul>        
        <li>
            <a href="@home.Url">@home.Name</a>
        </li>

        @foreach (var item in Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible() && x.IsDocumentType("Subfrontpage") || x.IsDocumentType("Procesguide")))
        {       


            var isSelected = item.IsDescendant(Model,"active", "");

            <li class="@Html.Raw(isSelected)">
                <a href="@item.Url">@item.Name</a>
            </li>

        }


    </ul>

【问题讨论】:

  • 它给出了什么错误?
  • 我在starttext中添加了错误

标签: razor umbraco


【解决方案1】:

你可以试试这样的:

var isSelected = item.IsDescendant(Model,"active", "");   

以下是您可以使用的功能列表:

@Model.AncestorOrSelf(string nodeTypeAlias)
@Model.AncestorOrSelf(int level)
@Model.AncestorOrSelf(Func<DynamicNode, bool> func)

还有那些 Helper 函数:

@Model.IsDescendant(DynamicNode[,valueIfTrue][,valueIfFalse])
@Model.IsDescendantOrSelf(DynamicNode[,valueIfTrue][,valueIfFalse])

参考:Is the current page a descendant of a specific node id?

以下是umbraco制作的默认导航脚本:

@inherits umbraco.MacroEngines.DynamicNodeContext

@*
    Macro to display child pages below the root page of a standard website.
    Also highlights the current active page/section in the navigation with
    the css class "current". 
*@


@{ 
    @*Get the root of the website *@
    var root = Model.AncestorOrSelf(1);
}

<ul>
    @foreach (var page in root.Children.Where("Visible"))
    { 
        <li class="@page.IsAncestorOrSelf(Model, "current", "")">
            <a href="@page.Url">@page.Name</a>
        </li>
    }
</ul>

【讨论】:

  • 我从导航脚本模板生成的默认脚本中添加了代码 sn-p。 screencast.com/t/EVJrGjM11yyj 如果当前节点位于其下,则此代码有效并将 current 类添加到主导航元素中。它应该可以帮助您完成您正在尝试做的事情。
  • 你会如何用强类型来写?
【解决方案2】:

好的,这就是解决方案。这是针对类型化的,而不是动态的 cshtml。

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
    var home = Model.Content.AncestorOrSelf(1);
}

<ul>    
    @*Render Home item*@

    @{ var homeActive = ""; }

    @if( home.Id == Model.Content.Id){
        homeActive = "active";
    }
    <li class="@homeActive">
        <a href="@home.Url">
            @home.Name               
        </a>
    </li>

    @*Render Home children*@    
    @foreach (var item in home.Children.Where(x => x.IsVisible()))
    {                       
        var active = "";

        if(home.Id != Model.Content.Id){ @* if NOT home *@
            if (item.Id == Model.Content.AncestorOrSelf(2).Id){ 
                @* if foreach id and currentpage ancestor id is equal  *@
                active = "active";
            }
        }          
        <li class="@active">                             
            <a href="@item.Url">
                @item.Name                   
            </a>
        </li>
    }
</ul>

【讨论】:

    【解决方案3】:

    我刚刚自己创建了一个菜单,并且还需要 Home 页面为 active。仅使用 IsAncestorOrSelf 方法不适用于主页。

    我创建的代码是这样的:

    @{ 
        var root = Model.AncestorOrSelf(1);
    
        var homeClass = root.Id == Model.Id ? "active" : "";
    }
    
    <ul id="nav" class="nav navbar-nav pull-right">
        <li class="@homeClass"><a href="/" >Home</a></li>
            @foreach (var page in root.Children.Where("Visible"))
        {
            <li class="@page.IsAncestorOrSelf(Model, "active", "")">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>
    

    这将遍历根节点 (1) 的所有子节点。为了能够发现您是否在 Home 页面上,我正在检查页面的 Id 并相应地设置 active 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多