Copied to clipboard

Flag this post as spam?

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


  • Kelsee Ishmael 71 posts 158 karma points
    Jun 10, 2015 @ 17:35
    Kelsee Ishmael
    0

    Global Helper TypedMedia Issue

    I'm trying to create a global helper file in the app code that will provide quick access to tasks. One of the helpers I'm trying to get to work doesn't seem to be referencing "Umbraco" properly.

    @using umbraco.MacroEngines;
    @using Archetype.Models;
    @using Archetype.Extensions;
    
    
    @helper getMediaSrc(string mediaID) {
        @Umbraco.TypedMedia(mediaID).Url
    }
    @helper getMediaAlt(string mediaID) {
        var img = Umbraco.TypedMedia(mediaID);
        img.GetPropertyValue<string>("alt");
    }
    @helper getMediaImg(string mediaID) {
        if (!String.IsNullOrEmpty(mediaID))
        {
            string src = getMediaSrc(mediaID).ToString() != String.Empty ? " src=\"" + getMediaSrc(mediaID) + "\"" : null;
            string alt = getMediaAlt(mediaID).ToString() != String.Empty ? " alt=\"" + getMediaAlt(mediaID) + "\"" : null;
            string imghtml = "<img" + src + alt + "/>";
            @Html.Raw(imghtml)
        }
    }
    

    When I use these helpers on the template and/or views, there is no issue, however in the global helper file, "TypedMedia" doesn't exist in the namespace. I've played around with change "Umbraco" to "Model", but it says that "Model" is null.

    I'm sure this is something simple..either an @inherits or @using I'm missing, but I'm new to Razor and seem to be lost on it.

    Thanks!

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 10, 2015 @ 19:16
    Dan Diplo
    101

    The UmbracoHelper object doesn't exist within files that aren't views or partials. So you have two options:

    You can either pass in a reference or create a new instance.

    To pass in a reference refactor your methods so they take an instance as a parameter. For example:

    Add these namespaces to the top of your helper file:

    @using umbraco.MacroEngines;
    @using Archetype.Models;
    @using Archetype.Extensions;
    @using Umbraco.Web;
    @using Umbraco.Core;
    @using Umbraco.Core.Models;
    
    @helper getMediaSrc(string mediaID, UmbracoHelper umbHelper)
    {
        @umbHelper.TypedMedia(mediaID).Url;
    }
    

    Then when you call your helper from a view pass in the Umbraco instance like this:

    @globalHelper.getMediaSrc("12", Umbraco);
    

    The alternative way is to create an instance of the helper in your methods like this:

    @helper getMediaSrc(string mediaID)
    {
        UmbracoHelper umbHelper = new UmbracoHelper();
        @umbHelper.TypedMedia(mediaID).Url;
    }
    

    I would also look at refactoring your code since it isn't particularly efficient - you are making multiple references to TypedMedia when you really should call it once, store the result in a variable, and then use the variable to access properties.

Please Sign in or register to post replies

Write your reply to:

Draft