Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save zealott/73fcf6ceedc7202dd9025f9aa01fc3b4 to your computer and use it in GitHub Desktop.

Select an option

Save zealott/73fcf6ceedc7202dd9025f9aa01fc3b4 to your computer and use it in GitHub Desktop.
Demonstration of how to select and loop through a list of components while calling the appropriate partial view.
// Get the components, this statement gets all of them from the LeftSideColumnComponent list regardless of component type
IEnumerable<ICMSElement> leftSideColumnComponents = Model.Elements(null, "LeftSideColumnComponent");
// Loop through components and call view that matches root element name.
foreach (ICMSElement leftSideColumnComponent in leftSideColumnComponents)
{
//add a try/catch in case a view is missing
String compName = leftSideColumnComponent.RootElementName;
try
{
//If you want the component to be editable & pagebuilder compatible, put it in Editable & name it with an _Unit
Html.RenderPartial("Editable/" + compName + "_Unit", leftSideColumnComponent);
//otherwise they can live in components
Html.RenderPartial("Components/" + compName, leftSideColumnComponent);
}
catch (Exception e)
{
// no view - output error in html comment if on runtime, or display it in designtime
@(Model.IsPreview ? e.Message : "")
<!-- Error displaying Editable/@compName: @e.Message-->
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment