3

Assume I have a menu with menu sections.

The menu has a title, opening paragraph, and a list of menu sections.

content
    title - Single line text
    paragraph - Rich text
    sections - Multilist

A menu section might be: Burgers, Pasta, Pizza.

I have a Model controller for the Menu.

The Model is just a copy of the fields in the Sitecore item:

  public string Title
  public string Paragraph
  public Item[] Sections

Index.cshtml looks like this:

<h2>@Model.Title</h2>
<div>@Model.Paragraph</div>
<hr />
@foreach (var section in Model.Sections)
{
  // how do I apply a rendering to a Sitecore item (id)?
}

And of course I would like to add / move / remove / modify Sections in the Experience Editor.

Rolan

  • Change your architecture, remove the multilist, add a placeholder where you want the sections to go, create a Section component and allow the content editor to add them in via the Experience Editor. – Richard Seal Nov 19 '19 at 16:31
  • If I understand correctly you want to add rendering to section item programmatically? Can you provide more detail here what you want achieve by doing this? may be someone can suggest more easy way to handle your requirement... any screenshot also if you have? – Yogesh Sharma Nov 19 '19 at 16:35
  • @RichardSeal If i remove the multilist, then what is the point of a multilist? I wanted the Menu item to know what sections it has, so maybe it could be used somewhere else, with its sections intact. – Rolan Christofferson Nov 19 '19 at 17:10
  • There are many reasons to use a multilist. Just not the use case you have I think. – Richard Seal Nov 19 '19 at 17:24
  • I agree. Simpler objects that don't need a whole rendering. Very new at this stuff. – Rolan Christofferson Nov 19 '19 at 18:09
1

What you want can be done like this:

<h2>@Model.Title</h2>
<div>@Model.Paragraph</div>
<hr />
@foreach (Sitecore.Data.Items.Item section in Model.Sections)
{
    @Html.Sitecore().Rendering("/sitecore/layout/Renderings/MySectionsRendering", new
    {
        DataSource = section.ID
    });
}

Side note: I do encourage you to work with placeholders in your Layout.cshtml and then adding the above code to a separate Rendering which is bound to the placeholder via standard values.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.