Content Menu Item: Add Image

Topics: General
Oct 2, 2013 at 9:19 PM
Hello!

I've added a Media Library Picker Field to the Content Menu Item Content Type. I'm trying to render the image in the MenuItemLink.cshtml but I'm unable to get to the Media Library Picker from the @Model object.

Any help will be very appreciated!

Image
Oct 4, 2013 at 5:37 PM
If anyone can help me over here --> =]
Coordinator
Oct 4, 2013 at 9:37 PM
What did you try and how did it not work? Should be something like this from the top of my head: Model.ContentItem.ContentMenuItem.MenuIcon.MediaParts to get to the list of media. Then if you want the first one, do FirstOrDefault() on that, test for null, and if not null, get its MediaUrl, Alternate text, etc, as needed.
Oct 4, 2013 at 10:50 PM
Edited Oct 4, 2013 at 10:50 PM
Bertrand, can't believe how close was I with what I was trying... You got me to the point!

The code ended up like this:
@{
    dynamic contentItem = @Model.Content.ContentItem;
}

@if(contentItem.ContentMenuItem.MenuIcon != null)
{
    if(contentItem.ContentMenuItem.MenuIcon.MediaParts.Length != 0)
    {
        <img src="@contentItem.ContentMenuItem.MenuIcon.MediaParts[0].MediaUrl" alt="image"/>
    }
}
<a href="@Model.Href">@Model.Text</a>
The result is something like this!

Image

I couldn't use the "FirstOrDefault()" extension method because MediaParts is of type System.Array.

Anyway, that's it! Thanks again!
Coordinator
Oct 5, 2013 at 12:02 AM
Yes you can use FirstOrDefault, or Any on an array, because arrays a re enumerable. You just have to bring in the namespace where those extensions methods are defined, with a using statement on top of your file.
Oct 5, 2013 at 12:21 AM
BertrandLeRoy wrote:
Yes you can use FirstOrDefault, or Any on an array, because arrays a re enumerable. You just have to bring in the namespace where those extensions methods are defined, with a using statement on top of your file.
Then can you please tell me what is wrong here?

http://s23.postimg.org/xade44gfv/Capture.png
Coordinator
Oct 5, 2013 at 12:30 AM
I think I already did. You're missing the using directive.
Oct 5, 2013 at 12:41 AM
Edited Oct 5, 2013 at 12:45 AM
Ignore this! :)
Oct 5, 2013 at 12:50 AM
Alright, tested everything and this throws the exact same error:
@using System.Linq;

@{
    dynamic contentItem = @Model.Content.ContentItem;
}

<a href="@Model.Href">
@if(contentItem.ContentMenuItem.MenuIcon != null)
{
    if(contentItem.ContentMenuItem.MenuIcon.MediaParts.Length != 0)
    {
        <img src="@contentItem.ContentMenuItem.MenuIcon.MediaParts.FirstOrDefault().MediaUrl" alt="image"/>
    }
}
@Model.Text</a>
Still missing something?
Coordinator
Oct 5, 2013 at 3:30 AM
Ah, yes, you need to cast it to enumerable, presumably, because there are some dynamics involved here.
Oct 9, 2013 at 3:49 PM
Roger that, thanks!