Copied to clipboard

Flag this post as spam?

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


  • Kevin 18 posts 108 karma points
    Jun 25, 2018 @ 18:03
    Kevin
    0

    Calling a property from inside a Multinode Treepicker

    I'm new to using MVC Views, Razor, and Macros so I'm not sure if I'm making a simple error, and I have some questions about general usage. Using Umbraco v7.11.0

    I'm trying to create an FAQ page where you can add Question-Answer pairs in the Backoffice and then choose which questions you want to publish in whatever order you choose. I thought using the Multinode Treepicker would let me do this easily.

    I set up these Document Types FAQ DocType Question Answer DocType

    QuestionAnswer is a child of FAQ.

    Then I made a PartialViewMacro ListFAQ.cshtml to try and query the Content page for QuestionAnswer DocType's properties under the Question tab.

    ListFAQ PartialViewMacro

    This code ends up listing all of the Q-A children instead of just the ones in the picked children. From what I understand, the Model.Content looks for all visible questionAnswers aliases under fAQ and then looks for the "question" and "answer" properties for each of the QuestionAnswer document types.

    The commented code is an attempt to call just the picked questions.

    I've seen a number of different ways to render a field from https://our.umbraco.com/documentation/reference/templating/Mvc/views but I still don't get when you would want to use @Umbraco.Field("") from @Model.Content.GetPropertyValue("")

    or why you might call it with @item.Name FeaturedProducts.cshtml

    When do you use these different forms to insert values or query data? How can I use this to get the data I want? Does this have anything to do with Dynamic or Strongly Typing? (which I've seen have different syntax from https://our.umbraco.com/projects/backoffice-extensions/terratype/bugs-questions/84071-umbracocoremodelsipublishedcontent-does-not-contain-a-definition-for-map)

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 21:24
    Frans de Jong
    0

    Just to clear some things up. Are you using Modelsbuilder?

  • Kevin 18 posts 108 karma points
    Jun 25, 2018 @ 21:31
    Kevin
    0

    I don't believe so. I've seen mentions of it as I've been learning Umbraco this past week, but I'm not too familiar with what it actually is. It helps make your Models Strongly Typed so you can use shorter calls?

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 21:51
    Frans de Jong
    2

    Yes, that's exactly it. It makes life a lot easier. Good explanation on modelsbuilder

    Personaly I only use modelsbuilder either in DLL for simple projects or with Modelsbuilder API in more complex projects.

    But now for your problem. When I don't use Modelsbuilder I always use HasProperty(""), HasValue("") and GetPropertyValue("") because I find it more readable (personal choice I guess).

    I expect the macro to be placed on the FAQ template. So Model.Content is the current IPublished content item (correct me if I'm wrong).

    So the basic way when not using Modelsbuilder would be something like this:

            //Check if the property "questions" has a value. Questions is the treepicker //containing the QuestionAnswer as IPublishedContent
            @if(Model.Content.HasPropertyValue("questions"))
            {
              //Loop through the items
              foreach(IPublishedContent questionAnswer in Model.Content.GetPropertyValue("questions"))
              {
                <section class="section">
                  <button class="accordion">@questionAnswer.GetPropertyValue("question")</button>
                  <div class="container-answer">
                    <p>@questionAnswer.GetPropertyValue("answer")</p>
                  </div>
                </section>
              }
            }
    

    Please let me know if this worked. I can give you a example in using modelsbuilder to but that will be tomorrow.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 21:53
    Frans de Jong
    1

    Here is more documentation about properties of IPublishedContent: Documentation

  • Kevin 18 posts 108 karma points
    Jun 25, 2018 @ 22:04
    Kevin
    0

    Error

    I got this error when previewing. The macro is indeed on the FAQ template using @Umbraco.RenderMacro("ListFAQ")

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 22:13
    Frans de Jong
    0

    Duh... I'm used to intelisense...

    It has to be HasValue() in the if

  • Kevin 18 posts 108 karma points
    Jun 25, 2018 @ 22:29
    Kevin
    0

    enter image description here

    This time with the foreach line with a different error message.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 22:33
    Frans de Jong
    100

    Can you try

    GetPropertyValue<IEnumerable<IPublishedContent>>("questions")
    
  • Kevin 18 posts 108 karma points
    Jun 26, 2018 @ 15:40
    Kevin
    0

    Yes that worked!

    So the Model.Content.GetPropertyValue fetches from the current page [FAQ] a custom property "questions" [the treepicker]. The property "questions" is an IEnumerable [meaning you can iterate through using a foreach] of IPublishedContent [a model for the content I need to render]?

    The end result is it grabs each of the treepicker items [QuestionAnswer].

    We call the field contents using .GetPropertyValue("") because each QuestionAnswer is an IPublishedContent.

    And this is how we'd write it without using ModelsBuilder.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 26, 2018 @ 15:44
    Frans de Jong
    0

    Exactly! My example leaves some room for improvement to check if a property exists or if it has a value. But essentially it's as simple as this

  • Kevin 18 posts 108 karma points
    Jun 26, 2018 @ 16:00
    Kevin
    0

    Excellent! Thank you so much! I think I understand the ModelsBuilder version from the link you've provided as well.

  • Kevin 18 posts 108 karma points
    Jun 25, 2018 @ 22:12
    Kevin
    0

    Modelsbuilder

    Okay so after following the link you posted I found this in the developer tab, and it looks like ModelsBuilder is enabled.

Please Sign in or register to post replies

Write your reply to:

Draft