Copied to clipboard

Flag this post as spam?

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


  • Harry Spyrou 199 posts 560 karma points
    Feb 07, 2019 @ 13:40
    Harry Spyrou
    0

    Get 'Parent/Child' Model from Nested Content with strongly typed models -7.13

    Hello,

    I have this view code:

    @using Myproject.Web.Models
    @model BannerTopGrayTextBottom
    
    <div class="story-page-interview">
        <div class="story-page-interview-content">
    
            @foreach (var qa in Model.QuestionsAnswers)
            {
               //don't want to cast this,
               //but I want to use the QuestionAnswer strongly typed model
                var qaModel = (QuestionAnswer)qa;
                <div class="story-page-interview-content--question">
                    <p>@(qaModel.Question)</p>
                </div>
                <div class="story-page-interview-content--answer">
                    <p>@(qaModel.Answer)</p>
                </div>
            }
        </div>
    </div>
    

    QuestionsAnswers in this case is a Nested Content Collection. I have a strongly typed model created by UmbracoModelsBuild Tool for it called QuestionAnswer.

    It has Model.Question and Model.Answer in there.

    If you see the code, I'm casting it because I know it's the correct model for the data. But I don't like it to put it simply.

    I'd rather get the model but I'm not quite sure how to do that.

    If I was working in a child view and wanted the parent model I'd do

    var parentModel = Model.Parent<ParentModelName>();
    

    How do I do the same for the nested content collection?

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft