Rendering Nested Content with Multiple Partial Views
Is there a good way you could recommend to use a separate partial view for each element within the nested content?
The pattern used here is to deliver a Partial View to the Front End Developer for each "component", also it would be good to reuse where a doctype is used "normally" outside of Nested Content.
So imagine I can add either a Paragraph, a YouTube video, or a Carousel to my nested content. The end user adds Paragraph 1, then a You Tube Video, then another paragraph, then a Carousel and then a final paragraph.
So then following the example in the excellent documentation I have the bit where I "Do my thang". That's great and I get it.
Should I just put a switch in there ? like
var things =
Model.GetPropertyValue<IEnumerable<IPublishedContent>>
(“myNestedContent”)
foreach(var thang in things) {
Switch (thang.DocTypeAlias) {
case "YouTube" :
@Html.Partial("_YouTube",thang);
*etc.*
}
}
or is there a neater cleverer way to do this that I'm not seeing?
I know it's a huge mistake to mention XSLT templates so I won't.
Also - really in this case this would be done in the controller, so really @Html.Partial would only exist in the View and I wouldn't really want to put this logic in the view. This is mean't for illustrative purposes only.
I don't think you need a switch. Just use the DocTypeAlias as part of a naming convention.
var things = Model.GetPropertyValue<IEnumerable<IPublishedContent>>(myNestedContent);
foreach(var thang in things) {
@Html.Partial("NestedContent/"+thang.DocTypeAlias, thang);
}
I am using the below approach as @David has said then no need to care about Switch.
Just create the partial with the same name as the DocumentTypeAlias
var sections = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("sections");
@foreach (var item in sections)
{
@Html.Partial("~/views/partials/" + item.ContentType.Alias + ".cshtml", item)
}
Rendering Nested Content with Multiple Partial Views
Is there a good way you could recommend to use a separate partial view for each element within the nested content?
The pattern used here is to deliver a Partial View to the Front End Developer for each "component", also it would be good to reuse where a doctype is used "normally" outside of Nested Content.
So imagine I can add either a Paragraph, a YouTube video, or a Carousel to my nested content. The end user adds Paragraph 1, then a You Tube Video, then another paragraph, then a Carousel and then a final paragraph.
So then following the example in the excellent documentation I have the bit where I "Do my thang". That's great and I get it.
Should I just put a switch in there ? like
or is there a neater cleverer way to do this that I'm not seeing?
I know it's a huge mistake to mention XSLT templates so I won't.
Also - really in this case this would be done in the controller, so really @Html.Partial would only exist in the View and I wouldn't really want to put this logic in the view. This is mean't for illustrative purposes only.
I don't think you need a switch. Just use the DocTypeAlias as part of a naming convention.
Hi John,
This is our method that we use at work:
}
Each document type is rendered with a different partial view.
Hope that helps,
Paulius
I am using the below approach as @David has said then no need to care about Switch. Just create the partial with the same name as the DocumentTypeAlias
Hi everyone.
In this case , how we can apply sorting. For example , I want Image on the top on one page , then image at the bottom on second page.
You can sort your NC. It will render in the same order as in the backoffice.
is working on a reply...