简体   繁体   English

在foreach - umbraco中通过id获取媒体项目

[英]Take Media items by id in foreach - umbraco

as I wrote in title I have problem with take Media in foreach. 正如我在标题中所写的那样,我对在foreach中使用Media有疑问。

This is my code: 这是我的代码:

@foreach (var item in @Model.Children.Where("Visible").OrderBy("UpdateDate"))
    {
        @{
            var link = new umbraco.cms.businesslogic.media.Media(item.MediaID).Children.FirstOrDefault().getProperty("umbracoFile").Value;
        }
        <div class="galleryListItem" style="background-image: url('/imageGen.ashx?image=@link&width=273&height=161');">
            <a href="@item.Url" class="link"></a>
            <div class="contentGalleryList">
                <div class="highlightGalleryList">@item.Name</div>
            </div>
        </div>
    }

and this line: 这一行:

@link = new umbraco.cms.businesslogic.media.Media(item.MediaID).FirstOrDefault().getProperty("umbracoFile").Value;

give me this error: 给我这个错误:

Error loading MacroEngine script (file: GalleryList.cshtml);

What can I do to that start work? 我该怎样做才能开始工作? Maybe it's some better solve of this problem? 也许这是一个更好的解决这个问题?

Any help would be appreciated. 任何帮助,将不胜感激。

I think something like this should work for you: (I'm not 100% sure what you code is trying to do, I've assumed the item.MediaID contains the id of a media item) 我觉得这样的事情对你有用:(我不是100%肯定你的代码试图做什么,我假设item.MediaID包含媒体项的id)

@foreach (var item in @Model.Children.Where("Visible").OrderBy("UpdateDate"))
{
    var media = Library.MediaById(item.MediaID);

       <div class="galleryListItem" style="background-image: url('/imageGen.ashx?image=@media.Url&width=273&height=161');">
        <a href="@item.Url" class="link"></a>
        <div class="contentGalleryList">
            <div class="highlightGalleryList">@item.Name</div>
        </div>
    </div>
}

If you need more info of how to do things in Razor in Umbraco download the invaluable umbraco DynamicNode razor cheatsheet 如果您需要更多关于如何在Umbraco Razor做事的信息,请下载无价的umbraco DynamicNode剃须刀备忘单

There's also lots of great information on the our.umbraco.org wiki our.umbraco.org wiki上还有很多很棒的信息

I solve it. 我解决了 The problem was that I have open code section in other code section. 问题是我在其他代码部分中有开放代码部分。

Working code: 工作代码:

@foreach (var item in @Model.Children.Where("Visible").OrderBy("UpdateDate"))
{
    var link = new umbraco.cms.businesslogic.media.Media(item.MediaID).Children.FirstOrDefault().getProperty("umbracoFile").Value;

    <div class="galleryListItem" style="background-image: url('/imageGen.ashx?image=@link&width=273&height=161');">
        <a href="@item.Url" class="link"></a>
        <div class="contentGalleryList">
            <div class="highlightGalleryList">@item.Name</div>
        </div>
    </div>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM