Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig Newland 2 posts 72 karma points
    Dec 02, 2015 @ 18:25
    Craig Newland
    0

    Accessing media inside of media

    I am having difficulty accessing media inside of media. Here is the partial view that works

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var mediaId = Model.MacroParameters["mediaId"]; }
    @if (mediaId != null)
    {
    
        var media = Umbraco.Media(mediaId);
        var selection = media.Children("HomePageTile");
    
        if (selection.Any())
        { 
            <div class="home-page feed packed" id="feed">
                @{
                    foreach (var item in selection)
                    {
                        var test = "";
                        var backgroundColor = "transparent";
                        var linkTarget = "self";
                        var isALink = false;
    
    
                        if (item.backgroundColor != null) {
                            backgroundColor = item.backgroundColor;
                        }
    
                        if (item.linkType == "External Link") {
                            linkTarget = "blank";
                        }
    
                        if (item.linkType == "External Link" || item.linkType == "Internal Link") {
                            isALink = true;
                        }
    
                        <div style="background-color: @backgroundColor;" class="grid-cell [email protected] [email protected] hide-if-small-screen-0">
    
                            @if (isALink) {
                                <a target="_@linkTarget" href="@item.linkUrl">
                                    <img class="" src="@item.image" alt="" title="">
                                 </a>
                            } else {
                                <img class="" src="@item.image" alt="" title="">
                            }
    
                            @Html.Raw(item.html)
                        </div>
                    }
                }
            </div>
        }
    }
    

    And if I add var image = Umbraco.Media(item.image); it breaks.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var mediaId = Model.MacroParameters["mediaId"]; }
    @if (mediaId != null)
    {
    
        var media = Umbraco.Media(mediaId);
        var selection = media.Children("HomePageTile");
    
        if (selection.Any())
        { 
            <div class="home-page feed packed" id="feed">
                @{
                    foreach (var item in selection)
                    {
                        var test = "";
                        var backgroundColor = "transparent";
                        var linkTarget = "self";
                        var isALink = false;
                        var image = Umbraco.Media(item.image);
    
                        if (item.backgroundColor != null) {
                            backgroundColor = item.backgroundColor;
                        }
    
                        if (item.linkType == "External Link") {
                            linkTarget = "blank";
                        }
    
                        if (item.linkType == "External Link" || item.linkType == "Internal Link") {
                            isALink = true;
                        }
    
                        <div style="background-color: @backgroundColor;" class="grid-cell [email protected] [email protected] hide-if-small-screen-0">
    
                            @if (isALink) {
                                <a target="_@linkTarget" href="@item.linkUrl">
                                    <img class="" src="@item.image" alt="" title="">
                                 </a>
                            } else {
                                <img class="" src="@item.image" alt="" title="">
                            }
    
                            @Html.Raw(item.html)
                        </div>
                    }
                }
            </div>
        }
    }
    

    Error loading Partial View script (file: ~/Views/MacroPartials/HomePageTiles.cshtml)

  • Dan White 206 posts 510 karma points c-trib
    Dec 02, 2015 @ 22:45
    Dan White
    0

    What kind of property is item.image? Umbraco.Media() is trying to query for another node.

    If you want to get child media items of item, you should be able to just do item.Children.

  • Craig Newland 2 posts 72 karma points
    Dec 03, 2015 @ 15:15
    Craig Newland
    0

    Image (image), Type: Media Picker

  • Dan White 206 posts 510 karma points c-trib
    Dec 03, 2015 @ 17:02
    Dan White
    0

    Have you check what type it's returning? If you have Property Value Converters installed it might be already be returning an IPublishedContent.

    Also, you should probably check if the property has a value before you try to use it. I don't work with the dynamic syntax often, but it may be throwing an error if the property is empty.

    Have you looked at https://our.umbraco.org/Documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Media-Picker

Please Sign in or register to post replies

Write your reply to:

Draft