简体   繁体   中英

Render children pages in umbraco

I'm trying to make an FAQ page. The content structure is like this:

  • FAQ -- Question 1 -- Question 2 -- Question 3

I can't seem to work out how to get the child templates to render though (I'm new to this).

FAQ Entries (Parent page)

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
    Layout = null;
    var faqContentId = (int)CurrentPage.faqContent;
}

<html>
    @Html.Partial("head")
    <body>
        @Html.Partial("header")

        <div class="content">
            @{
                foreach (var child in Umbraco.Content(1168).Children) {
                    // render children here
                }
            }
        </div>

        @Html.Partial("footer")
    </body>
</html>

FAQ Entry (Child pages)

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

<div class="accordion" id="accordionExample">
    <div class="card">
        <div class="card-header" id="headingOne">
            <h5 class="mb-0">
            <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                @Umbraco.Field("title")
            </button>
        </h5>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
        <div class="card-body">
            @Umbraco.Field("answer")
        </div>
    </div>
</div>

它应该类似于@ Html.Partial(“ FAQ Entry”,child),其中第二个参数是要传递到部分视图的模型(子对象)。

Welcome my dear Umbraco Dev. Let me point you to a few directions:

If you only want the solution, skip to number 3.

1) Refrain from using @inherits Umbraco.Web.Mvc.UmbracoTemplatePage as it will soon be deprecated. Use @inherits Umbraco.Web.Mvc.UmbracoViewPage instead which will get you strongly typed stuff in your view and your code will look like @Model.GetPropertyValue("myValue")

Even better, create a custom model or try using Umbraco ModelsBuilder

which will get you cleaner code and stronger bindings with strongly typed models. But that's for another day.

2) I would recommend not grabbing nodes by id Umbraco.Content('id').Children like this. The reason is that if you for some reason delete the node and then create it again later on with the same name, the id will be different and your code will break the website.

Look at this page where people pointed me to the correct way of traversing umbraco Umbraco traversing

It talks about speed but it basically shows you also the correct way to traverse. you could also find the rest you need within Umbraco's documentation.

3)To the actual question: I'm assuming that the page you're on is the parent and you just want to grab the children. Then it's as easy as :

var collection = Model.Content.Children();
@foreach (var child in collection ) {

    child.GetPropertyValue("yourValueAliasHere")
}

if you were using @inherits Umbraco.Web.Mvc.UmbracoViewPage you would have:

var collection = Model.Children();
@foreach (var child in Model.Children()) {

    child.GetPropertyValue("yourValueAliasHere")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM