Copied to clipboard

Flag this post as spam?

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


  • Johan 6 posts 89 karma points
    Feb 03, 2017 @ 12:43
    Johan
    1

    Get Media Picker value

    Hi everyone!

    I have a small problem where I don't know how to code this.

    I have created a Media Picker with name "Footer Image" and Alias "footer_image".

    I have the following content structure:

    • Content
      • en
        • Home

    The Media Picker is placed on the "en" page on a tab named "Content".

    I want to display the selected image from the Media Picker in my master template for my webpage.

    I have also created a macro that uses razor script and imported that into the master template.

    So basically from my razor script macro I want to get the selected image / image path from my Media Picker and then create an img tag or similar to show the image on the web page.

    Thx in advance!

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 03, 2017 @ 14:02
    Alex Skrypnyk
    0

    Hi Johan

    It's pretty typical issue with Umbraco. First of all you need - get "en" node, you can do it like that:

    var enNode = Umbraco.AssignedContentItem.AncestorOrSelf(2);
    
    var imgId = enNode.GetPropertyValue<int>("footer_image");
    
    var imgNode = Umbraco.TypedMedia(imgId);
    
    var imgUrl = imgNode.Url;
    

    Each row is like one step. I hope it will help you.

    Thanks,

    Alex

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 03, 2017 @ 14:04
    Alex Skrypnyk
    0

    All these lines can look like only one:

    var imgUrl = Umbraco.TypedMedia(Umbraco.AssignedContentItem.AncestorOrSelf(2).GetPropertyValue<int>("footer_image")).Url;
    

    Be aware about error checking.

    Thanks,

    Alex

  • Johan 6 posts 89 karma points
    Feb 03, 2017 @ 14:33
    Johan
    0

    Thanks for the reply though when copying your code i get an error.

    The type or namespace name 'TypedMedia' does not exist in the namespace 'Umbraco'.

    Don't know if I'm missing a @using reference or something?
    Got the error when I copied the one line code.

    When copying the multiline / steps code I get another error:
    The type or namespace name 'AssignedContentItem' does not exist in the namespace

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 03, 2017 @ 14:35
    Alex Skrypnyk
    0

    Hi Johan

    In which file are you coding?

    I'm using razor view with

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
  • Johan 6 posts 89 karma points
    Feb 03, 2017 @ 15:54
    Johan
    0

    Hi !

    I I'm using the cshtml file under macro script and not the mvc one. Though I also tried it with the MVC Partial View without success, only gets an error saying Error loading Partial View script (file: ~/Views/MacroPartials/footer-image.cshtml)

    It isn't really that important on what razor I use, if it's mvc or not though I only found MVC Partial View and that didn't work with your @inherit code.

    Thx in advance.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 03, 2017 @ 16:42
    Alex Skrypnyk
    0

    Hi Johan,

    Can you check Umbraco Logs?

    It's in Developers section if you have Diplo Trace logger or go to "App_Data/Logs/UmbracoTraceLog.txt" . Try to find something about this error and share with me.

    Thanks,

    Alex

  • Johan 6 posts 89 karma points
    Feb 06, 2017 @ 13:16
    Johan
    1

    Hello again Alex, I appreciate all the help I've got from you!

    I have checked now checked the App_Data/Logs/UmbracoTraceLog.txt log without any result. And I can't find a "Developers section" inside my install folder.

    Though, I have begun to suspect that I might be missing an assembly or something similar.

    First I tried to get the code, you provided, to run without any result and then I tested with your:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    As soon as I added that and had all other code commented out I got the Error loading partial view script.

    The default script @inherits tag is

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    So when I searched for logs and tried to troubleshoot I navigated inside the "Views" folder inside the root installation folder.

    There I found the file "Web.config" that has the following content:

    <?xml version="1.0"?>
    <configuration>
        <system.web.webPages.razor>
            <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <pages pageBaseType="System.Web.Mvc.WebViewPage">
                <namespaces>
                    <add namespace="System.Web.Mvc" />
                    <add namespace="System.Web.Mvc.Ajax" />
                    <add namespace="System.Web.Mvc.Html" />
                    <add namespace="System.Web.Routing" />
                    <add namespace="Umbraco.Web" />
                    <add namespace="Umbraco.Core" />
                    <add namespace="Umbraco.Core.Models" />
                    <add namespace="Umbraco.Web.Mvc" />
                </namespaces>
            </pages>
        </system.web.webPages.razor>
    
        <appSettings>
            <add key="webpages:Enabled" value="false" />
        </appSettings>
    
        <system.web>
            <httpHandlers>
                <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
            </httpHandlers>
    
            <pages
               validateRequest="false"
               pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
               pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
               userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    
               <controls>
                   <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
               </controls>
            </pages>
        </system.web>
    
        <system.webServer>
            <validation validateIntegratedModeConfiguration="false" />
    
            <handlers>
                <remove name="BlockViewHandler"/>
                <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
            </handlers>
        </system.webServer>
    </configuration>
    

    After I had seen the above files content I begun to wounder if I was missing an assembly file, so I navigated to the bin folder.
    In the bin folder I found the System.Web.Mvc files but no files with Umbraco.Web.Mvc.* the only files I found there for Umbraco.Web was Umbraco.Web.UI

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies