Copied to clipboard

Flag this post as spam?

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


  • Tim 25 posts 112 karma points
    May 11, 2017 @ 16:02
    Tim
    0

    Umbraco.Media(ID).mediafile not working on 7.6?

    Did something change? This code works fine in Umbraco 7.5.x, our new 7.6.x install results in blank strings.

    slide.image results in 1117 Umbraco.Media(slide.image).umbracoFile results in ' '

    <div class="carousel-img" style="background-image: url('@Umbraco.Media(slide.image).umbracoFile')"></div>
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 11, 2017 @ 16:04
    Alex Skrypnyk
    0

    Hi Tim

    Try this one:

    @Umbraco.TypedMedia(slide.image).Url
    
  • Tim 25 posts 112 karma points
    May 11, 2017 @ 16:06
    Tim
    0

    :( That breaks the page altogether

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 11, 2017 @ 16:09
    Alex Skrypnyk
    0

    What is the error? are you sure that result of slide.image is ID?

  • Tim 25 posts 112 karma points
    May 11, 2017 @ 18:59
    Tim
    0

    Ok sorry about that delay....here is my entire macro:

    <div id="[email protected]" class="carousel slide" data-ride="carousel">
        <div class="container">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            @{int count = -1;}
            @foreach(dynamic slide in sliderNode.Children) {
                count++;
                <li data-target="#[email protected]" data-slide-to="@count" class="@(count == 0 ? "active" : "")"></li>
            }
        </ol>
    
        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
    
            @{count = 0;}
    
            @foreach(dynamic slide in sliderNode.Children) {
                <div class="item @(count == 0 ? "active" : "")">
                    <div class="carousel-img" style="background-image: url('@Umbraco.Media(slide.Image).umbracoFile')"></div>
                    <div class="carousel-caption">
                        <h1>@slide.Title</h1>
                        <p>@slide.Tagline</p>
                    </div>
                </div>
                count++;
            }
    
        </div>
        </div>
    
        <!-- Controls -->
        <a class="left carousel-control" href="#[email protected]" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#[email protected]" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
    

    When I change @Umbraco.Media(slide.Image).umbracoFile to what you suggested I get the following error:

    Cannot perform runtime binding on a null reference Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

    on this line:

    @foreach(dynamic slide in sliderNode.Children) {
    

    Yet that same line exists further up the script no problem, and if I remove your suggestion and put back in the original line everything runs as normal but without images. I can verify in the raw HTML that all three slides are created, titles and taglines are visible, but the mediafile returns ''. And yes the 1117 that is returned is correct.

  • Alex Brown 129 posts 620 karma points
    May 12, 2017 @ 07:34
    Alex Brown
    0

    Have you tried using

    @Umbraco.Media(slide["image"]).Url
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 12, 2017 @ 08:30
    Jeavon Leopold
    0

    @Tim where does sliderNode come from?

  • Tim 25 posts 112 karma points
    May 12, 2017 @ 11:02
    Tim
    0

    My bad, I didn't include the top three lines, had attention split!!

    @inherits Umbraco.Web.Macros.PartialViewMacroPage 
    @{  
      var sliderNodeID = string.IsNullOrEmpty((string)Model.MacroParameters["NodeID"])? 0 : Convert.ToInt32(Model.MacroParameters["NodeID"]);
      dynamic sliderNode = Umbraco.Content(sliderNodeID); 
    }
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 12, 2017 @ 11:05
    Alex Skrypnyk
    0

    Hi Tim

    Try to print what is in slide.Image?

    Is it right id? Does this media item exist?

    Alex

  • Tim 25 posts 112 karma points
    May 12, 2017 @ 11:06
    Tim
    0

    @Alex Brown

    I tried your suggestion and that provides an empty string as well.

    <div class="carousel-inner" role="listbox">
    
    
    
            <div class="item active">
                <div class="carousel-img" style="background-image: url('')"></div>
                <div class="carousel-caption">
                    <h1>Slide 1</h1>
                        <p>Slide 1 Tagline</p>
                </div>
            </div>
            <div class="item">
                <div class="carousel-img" style="background-image: url('')"></div>
                <div class="carousel-caption">
                    <h1>Slide 2</h1>
                        <p>Slide 2 Tagline</p>
                </div>
            </div>
            <div class="item">
                <div class="carousel-img" style="background-image: url('')"></div>
                <div class="carousel-caption">
                    <h1>Slide 3</h1>
                        <p>Slide 3 Tagline</p>
                </div>
            </div>
    
        </div>
    

    I changed the line to use @slide["image"] to verify the ID is correct and here is the output.

    <div class="carousel-inner" role="listbox">
    
    
    
                <div class="item">
                    <div class="carousel-img" style="background-image: url('1117')"></div>
                    <div class="carousel-caption">
                        <h1>Slide 1</h1>
                        <p>Slide 1 Tagline</p>
                    </div>
                </div>
                <div class="item active left">
                    <div class="carousel-img" style="background-image: url('1118')"></div>
                    <div class="carousel-caption">
                        <h1>Slide 2</h1>
                        <p>Slide 2 Tagline</p>
                    </div>
                </div>
                <div class="item  next left">
                    <div class="carousel-img" style="background-image: url('1119')"></div>
                    <div class="carousel-caption">
                        <h1>Slide 3</h1>
                        <p>Slide 3 Tagline</p>
                    </div>
                </div>
    
        </div>
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 12, 2017 @ 11:10
    Jeavon Leopold
    100

    Give this a try

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var sliderNodeId = string.IsNullOrEmpty((string)Model.MacroParameters["NodeID"]) ? 0 : Convert.ToInt32(Model.MacroParameters["NodeID"]);
        var sliderNode = Umbraco.TypedContent(sliderNodeId);
    }
    
    <div id="[email protected]" class="carousel slide" data-ride="carousel">
        <div class="container">
            <!-- Indicators -->
            <ol class="carousel-indicators">
                @{int count = -1;}
                @foreach (var slide in sliderNode.Children)
                {
                    count++;
                    <li data-target="#[email protected]" data-slide-to="@count" class="@(count == 0 ? "active" : "")"></li>
                }
            </ol>
    
            <!-- Wrapper for slides -->
            <div class="carousel-inner" role="listbox">
    
                @{count = 0;}
    
                @foreach (var slide in sliderNode.Children)
                {
                    <div class="item @(count == 0 ? "active" : "")">
                        <div class="carousel-img" style="background-image: url('@(slide.GetPropertyValue<IPublishedContent>("image").Url)')"></div>
                        <div class="carousel-caption">
                            <h1>@(slide.GetPropertyValue<string>("title"))</h1>
                            <p>@(slide.GetPropertyValue<string>("tagline"))</p>
                        </div>
                    </div>
                    count++;
                }
    
            </div>
        </div>
    
        <!-- Controls -->
        <a class="left carousel-control" href="#[email protected]" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#[email protected]" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
    

    Umbraco v7.6 is by default geared to using Model.Content rather than CurrentPage and also includes converters so that content and media pickers return object rather than Id's

  • Tim 25 posts 112 karma points
    May 12, 2017 @ 11:17
    Tim
    0

    That did Jeavon!

    Looks like I'll have to study up on the new gearing! Thanks for the fix and point me to the knowledge!!!

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 12, 2017 @ 11:30
    Jeavon Leopold
    0

    No problem, it would also be worth looking into using Models Builder, that allows you to write evens simpler code

  • Tim 25 posts 112 karma points
    May 12, 2017 @ 12:03
    Tim
    0

    Never even heard of that, thanks!

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    May 12, 2017 @ 12:04
Please Sign in or register to post replies

Write your reply to:

Draft