1

I have a template with field of type treelist which holds images, i want to read this field in my view rendering (.cshtml file) and loop over the images and display each one in an tag. How can i do that?

Here is my code for reading from treelist: (this code was working fine when my view was a controller rendering, however when i tried to add a view rendering it gives me error: Inner Exception: The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'Sitecore.Data.Items.Item'. )

@model Sitecore.Data.Items.Item
@using Sitecore.Data.Fields
@using Sitecore.Data.Items
@using Sitecore.Resources.Media
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script type="text/javascript" src="~/Scripts/bootstrap.js"></script>
    <link rel="stylesheet" href="~/Content/bootstrap.css" />
    <meta name="viewport" content="width=device-width" />
    <title>

    </title>
</head>
<body>
  <div class="container">




         <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#myCarousel" data-slide-to="0"
                class="active"></li>
            <li data-target="#myCarousel" data-slide-to="1"></li>
            <li data-target="#myCarousel" data-slide-to="2"></li>
            <li data-target="#myCarousel" data-slide-to="3"></li>
        </ol>
        <div class="carousel-inner" role="listbox">
            @{
                IEnumerable<Item> heroImages = null;
                var heroImagesField = new MultilistField(
                Model.Fields["Hero Images"]);
                if (heroImagesField != null)
                {
                    heroImages = heroImagesField.GetItems();
                }
                if (heroImages != null)
                {
                    int i = 1;
                    foreach (var image in heroImages)
                    {
                        var mediaItem = (MediaItem)image;
                        <div class="item @(i == 1 ? "active" : "") ">
                            <img src=" @MediaManager.GetMediaUrl(mediaItem) "
                                 style="width:1920px;" />
                        </div>
                        i++;
                    }
                }
            }
        </div>
    </div>



</div>
</body>
</html>
1
  • Why move from a controller rendering to a view rendering? – Chris Auer May 28 '18 at 0:13
1

You should use the Sitecore.Mvc.Presentation.RenderingModel as your model and then retrieve a Sitecore context item from it, rather then defining the Sitecore.Data.Items.Item as a model.

So, you should change your model definition to this:

@model Sitecore.Mvc.Presentation.RenderingModel

P.S. Your question is covered quite well by this community forum q/a: https://community.sitecore.net/developers/f/8/t/109

Personally, I recommend looking through the community forum or Sitecore KB before submitting a question.

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.