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.
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?
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.
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:-
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:-
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
Hi Craig,
What you could do in your razor is something like
Then you can either output the name or id by doing
hope it helps
//fuji
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
Can you post the whole code?
//fuji
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")&width=150&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>
That didn't go well, sorry. It looked ok before I posted it!
I'll try again without trying to be clever with the formatting-
Craig are you getting any images displayed?
Can you try removing the @sign beforethe model here since you already globalise the
//fuji
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.
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
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.
I made a test earlier and its giving the name, id and NodeTypeAlias ("Folder") under V4.7.1.
Hope this helps.
//Fuji
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
Ok great- in that case use Fuji's method to get instantiate the Media but use childPage.MediaFolder instead of Model.MediaFolder.
-Tom
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:-
Just need to get to the name of the folder somehow.
Craig
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:-
Just need to get to the name of the folder somehow.
Craig
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!)
Craig:)
is working on a reply...