Copied to clipboard

Flag this post as spam?

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


  • Desley 44 posts 169 karma points
    Mar 12, 2014 @ 11:38
    Desley
    0

    AncestorsOrSelf()

    Hello Everyone,

    I have the following code to get the images from the Multiple Media Picker in my caroussel:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
            @if (Model.Content.HasValue("MultiMediaPicker"))
    {
        var imagesList = Model.Content.GetPropertyValue<string>("MultiMediaPicker").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
        var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
    
        foreach (var imageItem in imagesCollection)
            {      
                <li>
                    <img src="@imageItem.Url" />      
                </li>
            }                                                               
    }
    

    This works fine on the homepage but since the caroussel is the banner of the website it needs to work for every page. So I figured I would change the code appropiately to:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
            @if (CurrentPage.AncestorsOrSelf(1).First().HasValue("MultiMediaPicker"))
    {
        var imagesList = CurrentPage.AncestorsOrSelf(1).First().GetPropertyValue<string>("MultiMediaPicker").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
        var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
    
        foreach (var imageItem in imagesCollection)
            {      
                <li>
                    <img src="@imageItem.Url" />      
                </li>
    
    }  } 
    

    It does go into the if statement on every page but it can't find the MultiMediaPicker. Giving me the following error: ERROR LOADING PARTIAL VIEW SCRIPT (FILE: ~/VIEWS/MACROPARTIALS/MULTIMEDIAPICKER.CSHTML)

    Help will be greatly appreciated!

  • Yasir Butt 161 posts 371 karma points
    Mar 12, 2014 @ 12:08
    Yasir Butt
    0

    Hi,

    Can you use like it?

    var imageList = Model.Content.GetPropertyValue("MultiMediaPicker", true);

    if (imageList!=null){

    you code here

    }

    OR 

    if (Model.Content.HasValue("MultiMediaPicker", true))

    {

    }

     

  • Desley 44 posts 169 karma points
    Mar 12, 2014 @ 12:11
    Desley
    0

    If I use Model.Content is works on the homepage but not on any other pages. As such I am trying to use AncestorsOrSelf() or any other method to get the correct path on every page.

    If I use a combination of the two:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
            @if (CurrentPage.AncestorsOrSelf(1).First().HasValue("MultiMediaPicker"))
    {
        var imagesList = Model.Content.GetPropertyValue<string>("MultiMediaPicker").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
        var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
    
        foreach (var imageItem in imagesCollection)
            {      
                <li>
                    <img src="@imageItem.Url" />      
                </li>
            }                                                               
    }
    

    It works on the homepage and I get the following error on other pages: Object reference not set to an instance of an object.

  • Yasir Butt 161 posts 371 karma points
    Mar 12, 2014 @ 12:50
    Yasir Butt
    0

    I tried the below code and it works for me. it returns the image ids

    @{

        var imagelist = @Model.Content.GetPropertyValue("MultiMediaPicker", true);

    }

    I assume that your case is like.

    you have MultiMediaPicker property at root node/home and i made a partial macro as you and then i use the macro in below pages like about us and it works

     

    Can you please explain your scenerio?

     

    Other way is that you can create a pageid parameter in Macro where do you want to get the value.

     

    Yasir

     

    Yasir

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 12, 2014 @ 12:56
    Jeavon Leopold
    101

    Hi Desley,

    I would probably do this:

    @{
        var homepage = Model.Content.AncestorOrSelf(1);
    
        if (homepage.HasValue("MultiMediaPicker"))
        {
            var imagesList = homepage.GetPropertyValue<string>("MultiMediaPicker").Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
            var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
    
            foreach (var imageItem in imagesCollection)
            {
                <li>
                    <img src="@imageItem.Url" />
                </li>
            }
        }
    }         
    

    Jeavon

  • Desley 44 posts 169 karma points
    Mar 12, 2014 @ 13:35
    Desley
    0

    Thanks a bunch. Works perfectly.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 12, 2014 @ 23:23
    Jeavon Leopold
    0

    You're welcome!

  • Charles Afford 1163 posts 1709 karma points
    Mar 16, 2014 @ 14:48
    Charles Afford
    0

    Jeavon will that code will throw if the collection is empty?  If it trys to .split on nothing :).

    Charlie

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 16, 2014 @ 21:32
    Jeavon Leopold
    0

    @Charles, this snippet will not throw any exception. If it's empty then it doesn't "HasValue" so doesn't get past the first "if". There are multiple additional checks in here to avoid different exceptions.

    Removing all the checks you would be left with the snippet below, but it would throw exceptions in various situations.

    @{
        var homepage = Model.Content.AncestorOrSelf(1);
        var imagesList = homepage.GetPropertyValue("MultiMediaPicker").Split(',');
        var imagesCollection = Umbraco.TypedMedia(imagesList);
            foreach (var imageItem in imagesCollection)
            {
                <li>
                    <img src="@imageItem.Url" />
                </li>
            }     
    }     
    

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft