4

こんにちは、アクティブな場合、またはその下のページにいる場合は、「アクティブ」のクラスを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)">

私は今これを試しましたが、ランクはありません

<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>
4

3 に答える 3

0

私は自分でメニューを作成したばかりで、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

于 2014-03-29T12:42:31.507 に答える
0

次のようなことを試すことができます:

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

使用できる関数のリストは次のとおりです。

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

およびそれらのヘルパー関数:

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

参照:現在のページは特定のノード 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>
于 2013-04-12T14:27:30.107 に答える