Copied to clipboard

Flag this post as spam?

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


  • Glenn Franquet 18 posts 71 karma points
    Apr 29, 2011 @ 17:56
    Glenn Franquet
    0

    Get media items from a property with uComponent MultiNodePicker

    I have a page where I want to display information from some child pages.  In this child pages there is a field based on a datatype using uComponent MultiNodePicer.  In this field I selected some images and I want to display the different images.  Unfortunately I have some problems and don't find a solution. 

    I tried already some things from the topic http://our.umbraco.org/forum/developers/razor/17897-How-to-iterate-DynamicXml but without any success.

    The code on my template looks like

    <umbraco:Macro  runat="server" language="cshtml">
    @inherits umbraco.MacroEngines.DynamicNodeContext

    <ul>
    @foreach (var item in @Model.Children.Where("Visible")) {
    <li>Titel: @item.Name</li>
    <li>Text Left: @item.bodyText</li>
    <li>Text Right: @item.bodyTextRight</li>

    foreach (var image in item.contentImages) {
    <li>@image.nodeId </li>
    }
    }
    </ul>
    </umbraco:Macro>

    The error I get when I try this is "'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'nodeId'"

    I replaced the second foreach with the following line of code:

    <li>@item.contentImages.item[0].nodeId</li>

    this was mentioned in following topic: http://our.umbraco.org/forum/developers/razor/17729-Razor-newbie but then I get the message "'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'item'".

    I hope that I can get some advice about this because I really don't find the good solution.  It's probably something small I don't see after trying to long.

    Thanks all

    Glenn

  • Glenn Franquet 18 posts 71 karma points
    May 04, 2011 @ 11:32
    Glenn Franquet
    0

    I was able to solve this.

    To test I wrote out the InnerText for the property based on the MultiNodePicker and noticed that it displayed directly the nodeid of the image.
    So I changed my code to:

    foreach (var image in item.contentImages) {
    dynamic mediaItem = new DynamicMedia(image.InnerText);
      <li><img src='/[email protected]&width=300&height=150' />  </li>
    }

    I'm not sure if this is the correct way of doing this, but it seems to work for me.

  • Mads Krohn 211 posts 504 karma points c-trib
    May 05, 2011 @ 22:35
    Mads Krohn
    0

    If your solution works for you, then all is good :)

    However, a quick pointer to the Model.MediaById() method.

    Taken from part 5 of the blog post series by Gareth Evans, it is possible to do the following:

    @{
    var images = Model.MediaById(Model.yourMultiPickerPropertyName);
    }

    This will only work if you return your value as csv, not xml!
    MediaById (and also NodeById) takes a params object[], which will return a DynamicMediaList.

    Words of warning though, DynamicMediaList is just a list wrapper, it doesn't support OrderBy or Where like DynamicNodeList does.

    The post can be found here

  • Glenn Franquet 18 posts 71 karma points
    May 12, 2011 @ 14:26
    Glenn Franquet
    0

    I took some time to test the Model.MediaById solution.
    This works but not when there is only one media item selected.

    Following error is returned when looping over the items in a foreach when there is only one media item:
    Cannot implicitly convert type 'umbraco.MacroEngines.DynamicMedia' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

    This is propably because of the different overload methods for MediaById.
    There are some methods that return umbraco.MacroEngines.DynamicMediaList (multiple media items)
    And other methods return umbraco.MacroEngines.DynamicMedia (only 1 media item)

    When there is only one media item then the methode MediaById(int) is called and this return DynamicMedia instead of DynamicMediaList (MediaById(object[])

    A solution could be to test first the type of the variable that was filled by the MediaById methode before doing some other actions with this variable.  But maybe a more interesting solution for the future would be that the functions who return a MediaList have a pluralised name (ex MediaItemsById)

    My implementation is now the following:

    @if (item.GetProperty("contentImages").Value != "")
    {
        var images = Model.MediaById(item.contentImages);

        if (images is umbraco.MacroEngines.DynamicMedia)
        {
            <img src='/[email protected]&width=342&height=232' />
        }
        else
        {
        <ul>
                    @foreach (var image in images)
                    {
                        <li><img src='/[email protected]&width=342&height=232' /></li>
                    }
        </ul>
        }
    }

     

  • Mads Krohn 211 posts 504 karma points c-trib
    May 12, 2011 @ 14:34
    Mads Krohn
    0

    Interesting find!

    Im guessing though, that it will call MediaById(string) and not MediaById(int). 

    It's good to see that you found your own workaround, though, another solution could be to always cast the string to an array like this:

    var images = Model.MediaById(item.contentImages.ToArray());

    That way you would get an array of strings even when there is only one node selected.
    Let me know if that works out for you.

  • Glenn Franquet 18 posts 71 karma points
    May 12, 2011 @ 14:46
    Glenn Franquet
    0

    It seems to be MediaById(int).

    I get the message "'int' does not contain a definition for 'ToArray'".
    Casting first to a string (item.contentImages.String().ToArray()) returns the error "'System.Collections.Generic.List' does not contain a definition for 'String'"

  • Mads Krohn 211 posts 504 karma points c-trib
    May 12, 2011 @ 15:04
    Mads Krohn
    0

    Haha nice :)

    Ok, så apparently, the data type returned is different depending on the number of nodes checked on the field.
    I did a quick look through the DynamicNode source code, and apparently, it tries to parse the value into an appropriate type.
    As this is the case, your errors makes perfect sense.

    Combining the different return values from both MediaById and the properties on @model, I guess this makes the MediaById method kinda flawed.

    Lets hope Gareth Evans stops by with a good suggestion on how to move on from here.

  • Dennis M Dewey 15 posts 35 karma points
    Nov 08, 2011 @ 05:55
    Dennis M Dewey
    0

    Hi Glenn, I'm trying to get this to work on my site. I'm still a noob to Umbraco so please bear with me. 

    At first I didn't want to do it like this because it is using both Imagegen and uComponents, but the uComponents documentation was not helpful at all so I've been tinkering with this method. It's giving me an error right now:

    c:\HostingSpaces\xxxx\xxxx.com\wwwroot\macroScripts\634562957958785063_Slideshow.cshtml(63): error CS0117: 'umbraco.item' does not contain a definition for 'GetProperty'

  • Glenn Franquet 18 posts 71 karma points
    Nov 09, 2011 @ 17:01
    Glenn Franquet
    0

    Hello Dennis,

    were you able to solve this issue?  If not, can you post your code here so that we can take a look on what could causes the error.

  • Dennis M Dewey 15 posts 35 karma points
    Jan 06, 2012 @ 06:51
    Dennis M Dewey
    0

    I was looking over my posts and noticed that I did not reply to this. I ended up using DAMP to finish the slideshow. I don't know why I was trying with uComponents MultiNodePicker when it was so easy to do with Digibiz Advanced Media Picker. That should help to explain how much I've learned after a couple of months of using Umbraco. My slideshow is an implementation of a jquery slideshow called "coin slider" that can be found at http://workshop.rs/projects/coin-slider/.

    Here is my code :

            if (Model.HasValue("HomePageSlideShow")) {
                dynamic mediaItems = Model.HomePageSlideShow.mediaItem;
                if (mediaItems.Count() != 0)
                {
                    <div id="coin-slider">
                    @foreach (var item in mediaItems)
                    {
                        var image = item.Image;
                        <href="@image.mediaURL"><img src="@image.umbracoFile" alt="@image.nodeName" />a>
                    }
                    div>
                    <script>$(document).ready(function({
                    $('#coin-slider').coinslider();
                    });
                    script>
                }

     

Please Sign in or register to post replies

Write your reply to:

Draft