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
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.
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 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?
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.
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.
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
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.
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
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)
Just to clear some things up. Are you using Modelsbuilder?
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?
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("")
andGetPropertyValue("")
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:
Please let me know if this worked. I can give you a example in using modelsbuilder to but that will be tomorrow.
Here is more documentation about properties of IPublishedContent: Documentation
I got this error when previewing. The macro is indeed on the FAQ template using @Umbraco.RenderMacro("ListFAQ")
Duh... I'm used to intelisense...
It has to be HasValue() in the if
This time with the foreach line with a different error message.
Can you try
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.
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
Excellent! Thank you so much! I think I understand the ModelsBuilder version from the link you've provided as well.
Okay so after following the link you posted I found this in the developer tab, and it looks like ModelsBuilder is enabled.
is working on a reply...