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>