我使用NuGet和VS2013安装了Umbraco 7.1.5。我有一个MainMenu局部视图,它获取文档树并相应地填充菜单:
var homePage = CurrentPage.AncestorsOrSelf(1).First();
var menuItems = homePage.Children.Where("UmbracoNaviHide == false");
<ul class="nav">
<li class="@(CurrentPage.Url == "/" ? "current_page_item" : null)"><a class="Lvl1 home" href="/">Home</a></li>
@foreach (var item in menuItems)
{
<li class="@(CurrentPage.Id == item.Id ? "current_page_item" : null) Col1">
<a class="lvl1 parent" href="@item.Url">@item.Name</a>
@{var subMenuItems = item.Children.Where("UmbracoNaviHide == false");}
@if (subMenuItems.Count() > 0)
{
<ul>
@foreach (var sub in subMenuItems)
{
???? @Umbraco.Field("menuItemImage", recursive: true) ?????
<li><a class="parent" href="@sub.Url">@sub.Name</a></li>
}
</ul>
}
</li>
}
</ul>
问:如何为第二个foreach中的每个子菜单项获取@ Umbraco.Field(“menuItemImage”,递归:true)(上传类型)?
答案 0 :(得分:1)
使用来自IPublishedContent接口的GetPropertyValue。
@foreach (var sub in subMenuItems)
{
// will only work if image is mandatory, otherwise the inner value can be null.
// In that case you need to add an extra check
var img = Umbraco.TypedMedia(sub.GetPropertyValue<string>("menuItemImage", recurse:true))
<img src="@img.Url" alt="sub.Name" />
<li><a class="parent" href="@sub.Url">@sub.Name</a></li>
}
<强>更新强>
因为您正在使用动力学,所以您也可以@sub.menuItemImage
。因为我不太喜欢动态,所以我经常使用Model.Content
代替CurrentPage
。就像那样,由于Model.Content
强类型,你将在visual studio中收到智能感知。