Item corresponding to RenderingView

Hello,

 

I am trying to use a rending view to generate a side menu,

Here is my item hierarchy:

/sitecore/Content/Home/Products/Phones

Basically, under phones, there will be iphone6,iphone7, iphone8, those would be the links in the side menu.

when I try to run this, basically, nothing is rendered in the main layout.

if I change:

@foreach (var item in Model.Item.Parent.Children.ToArray())

to

@foreach (var item in Sitecore.Context.Database.GetItem("/sitecore/Content/Home/Products/Phones").Children.ToArray())

My side view will render successfully without issues.

 

I put following testing output in my rendering view layout:

@Html.Sitecore().Field("Title", @item)

 

For this value, in the preview, it renders: Site Experience Platform.Home1, while Home1 is the title for my context item Home

I am really struggling to figure out where does the following points to in the item hierarchy? Can someone explain?

Model.Item.Parent.Children.ToArray()

 

 

 

 

Below is all the codes for my rendering view (SideMenu.cshtml)

 

@using Sitecore.Mvc.Presentation

@model RenderingModel

<div>

<ol class="sidemenu">

@*@foreach (var item in Sitecore.Context.Database.GetItem("/sitecore/Content/Home/Products/Phones").Children.ToArray())*@

 

@foreach (var item in Model.Item.Parent.Children.ToArray())

{

 

@Html.Sitecore().Field("Title", @item)

if (@item["Show in Menu"] == "1")

{

<li>

<a href="@Sitecore.Links.LinkManager.GetItemUrl(@item)">

@item.Fields["Title"].Value

</a>

</li>

}

}

</ol>

</div>

 

 

Below is my main layout to render the rending view (main.cshtml):

 

<!DOCTYPE html>

<html>

<head>

<title>@Html.Sitecore().Field("Title", Sitecore.Context.Item, new { DisableWebEdit = true })</title>

<link rel="stylesheet" href="/css/bootstrap.min.css">

<link href="/css/desktop.css" rel="stylesheet" />

<script src="/js/jquery.min.js"></script>

<script src="/js/bootstrap.min.js"></script>

</head>

<body>

<div>

<div id="header">

<a href="/">

@Html.Sitecore().Field("Logo", Sitecore.Context.Database.GetItem("/sitecore/Content/Global/Configurations"))

</a>

</div>

<div id="sidemenu">

@Html.Sitecore().Rendering("{64823336-BE63-4AC1-A09F-896BBE470303}")

</div>

<div id="contentarea">

<h1>@Html.Sitecore().Field("Title", Sitecore.Context.Item, new { DisableWebEdit = true })</h1>

@Html.Sitecore().Field("Body")

</div>

<div id="footer">

@Html.Sitecore().Field("Copyright Text", Sitecore.Context.Database.GetItem("/sitecore/Content/Global/Configurations"))

</div>

</div>

</body>

</html>

 

Thanks a lot and have a good night,

 

Jiao

  • Hi Jiao,  I would suggest you to check few thing  -

    Easiest way to achieve  this provide the datasource to that rendering for node  - /sitecore/Content/Home/Products/Phones and then code will be as given below

     

    @*@foreach (var item in Model.Item.Children)*@

     

    It will give you the list of child items of Phone item.  -  I would recommend this way because in this way you can use the same rendering to any other place of the site and change the content by changing the datasource.

     

    Note - Apply the datasource of Template standard value so it will be applied on all items

     

    Another way (Not recommended)

    1. Remove the datasource from rendering and go with below code.

     

    @*@{

    var contextItem=Sitecore.Context.Item;

    }*@

     

    @*@foreach (var item in contextItem.Parent.Children)*@

     

    Please make this change and let me know if you are still having same issue.