Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Jun 09, 2012 @ 17:56
    Craig100
    0

    Get MediaFolder name from MediaFolder ID

    Hi,

    In a razor macro Model.MediaFolder gives me the ID of the MediaFolder. How do I get the Name (or value) associated with it. I've tried 

    var mediaFolderName = Library.MediaById(Model.MediaFolder);

    With no luck

    MediaFolder is a media picker where I select a media folder. It saves the name of the folder in the media picker, I want to get the name of the folder in my macro.

    Any help appreciated.

    Craig

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 10, 2012 @ 07:19
    Fuji Kusaka
    1

    Hi Craig,

    What you could do in your razor is something like

    var mediaFolderName = Model.MediaById(Model.MediaFolder);

    Then you can either output the name or id by doing

    <p>@mediaFolderName.Name></p>
    <p>@mediaFolderName.id</p>

    hope it helps

    //fuji

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 11:55
    Craig100
    0

    Thanks Fuji but that didn't work either:(

    If I use @mediaFolderName.Name I get nothing at all displayed.  If I use @mediaFoldName.Id I get "0" displayed.

    Craig

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 10, 2012 @ 12:00
    Fuji Kusaka
    0

    Can you post the whole code?

     

    //fuji

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 12:11
    Craig100
    0

    Sure

    <nav class="masterMenu"> <ul id="nav"> @{ var homeNode = Model.AncestorOrSelf("HomePage"); } @foreach (var page in homeNode.Children.Where("Visible")) { <li @Library.If(page.Id == Model.Id, "class=\"activeItem1\"","class=\"masterItem\"")> @if(page.NodeTypeAlias != "SectionContainer") { <a href="@page.Url" title="@page.ContentTitle">@page.ContentTitle</a> } else { <a href="#" title="@page.ContentTitle">@page.ContentTitle</a> } @if (page.Children.Where("Visible").Count() > 0) { if(page.hideSubNav != true){ <ul class="subNav"> @foreach (var childPage in page.Children.Where("Visible")) { var mediaFolderName = @Model.MediaById(Model.MediaFolder); <li> <figure> <a href="@childPage.Url" title="@childPage.ContentTitle"><img src="/[email protected]("thumbnail","umbracoFile")&amp;width=150&amp;height=120" width="150" height="120" alt="@childPage.ContentTitle"></a><br> <figcaption> <a href="@childPage.Url" title="@childPage.ContentTitle">@mediaFolderName.Id</a> </figcaption> </figure> </li> } </ul> } } </li> } </ul> </nav> 

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 12:12
    Craig100
    0

    That didn't go well, sorry. It looked ok before I posted it!

     

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 12:53
    Craig100
    0

    I'll try again without trying to be clever with the formatting-

    <nav class="masterMenu">
        <ul id="nav">
    
            @{ var homeNode = Model.AncestorOrSelf("HomePage"); }        
    
            @foreach (var page in homeNode.Children.Where("Visible"))
            {
                <li @Library.If(page.Id == Model.Id, "class=\"activeItem1\"","class=\"masterItem\"")>
                   @if(page.NodeTypeAlias != "SectionContainer")
                    {
                      <a href="@page.Url" title="@page.ContentTitle">@page.ContentTitle</a>            
                    }
                    else
                    {
                      <a href="#" title="@page.ContentTitle">@page.ContentTitle</a>
                    }
                    @if (page.Children.Where("Visible").Count() > 0)
                    {
                     if(page.hideSubNav != true){
                        <ul class="subNav">
                            @foreach (var childPage in page.Children.Where("Visible"))
                            {
                             var mediaFolderName = @Model.MediaById(Model.MediaFolder);
                             <li>
                                <figure>                         
                                  <a href="@childPage.Url" title="@childPage.ContentTitle"><img src="/[email protected]("thumbnail","umbracoFile")&amp;width=150&amp;height=120" width="150" height="120" alt="@childPage.ContentTitle"></a><br>
                                  <figcaption>
                                   <a href="@childPage.Url" title="@childPage.ContentTitle">@mediaFolderName.Id</a>
                                  </figcaption>
                                </figure>
                             </li>
                            }
                        </ul>
                        }
                    }
                </li>  
            }
        </ul>
    </nav>
  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 10, 2012 @ 13:23
    Fuji Kusaka
    0

    Craig are you getting any images displayed?

    Can you try removing  the @sign beforethe model here since you already globalise the

    var mediaFolderName = Model.MediaById(Model.MediaFolder);

    //fuji

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 13:38
    Craig100
    0

    Everything works wonderfully apart from not getting the .Name (or .Id) of the media folder. Removing the "@" didn't make any difference:(

    The media set up is:-

    Media/Galleries/Limited Editions/2009 (/2010,/2011,/2012)

    So I'm expecting to see "2009" or "2010", etc. as the mediaFolderName.

    "mediaFolder" is the alias of the Media Picker in the Document Type for the page.

    Craig

    P.S. it's an Umbraco 4.7.2 site.

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 10, 2012 @ 15:07
    Tom Fulton
    0

    Hi,

    What Fuji said initially should work, but it sounds like maybe your MediaFolder field is empty if it's returning an ID of 0.  Can you try:  <p>ID: @Model.MediaFolder</p>  and see what you get back?

    Also I'm not sure if this is by design, but your for each loop is accessing media from the childpage for the image, but your MediaFolder property is coming from Model (CurrentPage) - not sure if that should be coming from the subpage?

    -Tom

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 10, 2012 @ 15:12
    Fuji Kusaka
    0

    Craig,

    I havent worked with v 4.7.2 yet, but i dont think that would make a big difference the way of calling the Name of the folder.

    Can you try somthing instead and see what is the outcome with a new macro.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{    
         var mediaFolderName = Model.MediaById(Model.MediaFolder);
    <p>slider: @mediaFolderName.NodeTypeAlias</p>
    <p>slider: @mediaFolderName.Name</p>
    <p>@mediaFolderName.NodeTypeAlias</p>
    }

    I made a test earlier and its giving the name, id and NodeTypeAlias ("Folder") under V4.7.1.

    Hope this helps.

    //Fuji

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 15:19
    Craig100
    0

    Thanks Tom,

    It seems you are right.

    ID: @Model.MediaFolder gives nothing at all.

    However if I change it to ID: @childPage.MediaFolder I get ID: 117 (the ID of the folder).

    Now the challenge is to convert the ID to the Name. I tried @childPage.MediaFolder.Name and @childPage.MediaFolder.NodeName but each causes a catastrophic error. I wish I knew of somewhere I could look to see the node names/XML so it wasn't so hit and miss.

    Craig

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 10, 2012 @ 15:28
    Tom Fulton
    1

    Ok great-  in that case use Fuji's method to get instantiate the Media but use childPage.MediaFolder instead of Model.MediaFolder.

    var yourMediaFolder = Library.MediaById(childPage.MediaFolder);
    <p>name: @yourMediaFolder.Name</p>

    -Tom 

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 15:32
    Craig100
    0

    Hi Tom,

    It appears you are right about the child page. ID: @Model.MediaFolder gives nothing at all while ID: @childPage.MediaFolder give the folder ID's.

    If I use ID: @MediaFolderName I get "umbraco.MacroEngines.DynamicNode" output for each image caption.

    Also tried changing var mediaFolderName = childPage.MediaById(Model.MediaFolder); to var mediaFolderName = childPage.MediaById(childPage.MediaFolder); but that just gave the "umbraco.MacroEngines.DynamicNode" output for each image caption as well.

    Fuji - Following your suggestion I got the code to output the work "Folder" as follows:-

                        <ul class="subNav">
                            @foreach (var childPage in page.Children.Where("Visible"))
                            {
                             var mediaFolderName = childPage.MediaById(childPage.MediaFolder);
                             <li>
                                <figure>                         
                                  <a href="@childPage.Url" title="@childPage.ContentTitle"><img src="/[email protected]("thumbnail","umbracoFile")&amp;width=150&amp;height=120" width="150" height="120" alt="@childPage.ContentTitle"></a><br>
                                  <figcaption>
                                   <a href="@childPage.Url" title="@childPage.ContentTitle">ID: @mediaFolderName.NodeTypeAlias</a>
                                  </figcaption>
                                </figure>
                             </li>
                            }
                        </ul>

    Just need to get to the name of the folder somehow.

    Craig

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 15:33
    Craig100
    0

    Hi Tom,

    It appears you are right about the child page. ID: @Model.MediaFolder gives nothing at all while ID: @childPage.MediaFolder give the folder ID's.

    If I use ID: @MediaFolderName I get "umbraco.MacroEngines.DynamicNode" output for each image caption.

    Also tried changing var mediaFolderName = childPage.MediaById(Model.MediaFolder); to var mediaFolderName = childPage.MediaById(childPage.MediaFolder); but that just gave the "umbraco.MacroEngines.DynamicNode" output for each image caption as well.

    Fuji - Following your suggestion I got the code to output the word "Folder" as follows:-

                        <ul class="subNav">
                            @foreach (var childPage in page.Children.Where("Visible"))
                            {
                             var mediaFolderName = childPage.MediaById(childPage.MediaFolder);
                             <li>
                                <figure>                         
                                  <a href="@childPage.Url" title="@childPage.ContentTitle"><img src="/[email protected]("thumbnail","umbracoFile")&amp;width=150&amp;height=120" width="150" height="120" alt="@childPage.ContentTitle"></a><br>
                                  <figcaption>
                                   <a href="@childPage.Url" title="@childPage.ContentTitle">ID: @mediaFolderName.NodeTypeAlias</a>
                                  </figcaption>
                                </figure>
                             </li>
                            }
                        </ul>

    Just need to get to the name of the folder somehow.

    Craig

  • Craig100 1136 posts 2523 karma points c-trib
    Jun 10, 2012 @ 16:05
    Craig100
    0

    Things seem to be well ef'd up with this forum (layout, double postings, etc.). Never have this trouble with any others!

    Anyway... All working now. Thanks Tom and Fuji, couldn't have done it without you;)

    Just for completeness, here's the final code:- (hope it all posts ok!)

    <nav class="masterMenu">
        <ul id="nav">
    
            @{ var homeNode = Model.AncestorOrSelf("HomePage"); }        
    
            @foreach (var page in homeNode.Children.Where("Visible"))
            {
                <li @Library.If(page.Id == Model.Id, "class=\"activeItem1\"","class=\"masterItem\"")>
                   @if(page.NodeTypeAlias != "SectionContainer")
                    {
                      <a href="@page.Url" title="@page.ContentTitle">@page.ContentTitle</a>            
                    }
                    else
                    {
                      <a href="#" title="@page.ContentTitle">@page.ContentTitle</a>
                    }
                    @if (page.Children.Where("Visible").Count() > 0)
                    {
                     if(page.hideSubNav != true){
                        <ul class="subNav">
                            @foreach (var childPage in page.Children.Where("Visible"))
                            {
                             var mediaFolderName = Library.MediaById(childPage.MediaFolder);
                             <li>
                                <figure>                         
                                  <a href="@childPage.Url" title="@childPage.ContentTitle"><img src="/[email protected]("thumbnail","umbracoFile")&amp;width=150&amp;height=120" width="150" height="120" alt="@childPage.ContentTitle"></a><br>
                                  <figcaption>
                                   <a href="@childPage.Url" title="@childPage.ContentTitle">ID: @mediaFolderName.Name</a>
                                  </figcaption>
                                </figure>
                             </li>
                            }
                        </ul>
                        }
                    }
                </li>  
            }
        </ul>
    </nav>

     

    Craig:)

Please Sign in or register to post replies

Write your reply to:

Draft