Copied to clipboard

Flag this post as spam?

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


  • Rich W 4 posts 84 karma points
    18 days ago
    Rich W
    0

    Multiple Media Picker in Macro Parameter problem

    I am looking to create a macro that uses the multiple media picker as a parameter. For some reason my marco partial view refuses to see the images in the parameter value. I've set the code to test up as follows:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web;
    @using Umbraco.Web.Composing;
    @using Clean.Core.Helpers;
    @{
    var images = Model.MacroParameters["imageSlide"] as IEnumerable<IPublishedElement>;
    }
    @if (images != null)
    {
    <ul>
    @foreach (var item in images) {
    <li>Test</li>
    }
    </ul>
    }
    else{
    <p>nope</p>
    }
    

    I'm not sure how to get the partial to output the test li tags in this example. it always returns the else statement,

  • Marc Goodson 1584 posts 10551 karma points MVP 6x c-trib
    17 days ago
    Marc Goodson
    0

    Hi Rich W

    The way Macro Parameters are 'read' it doesn't go via a Property Value Converter in the same way that reading a property value from a Document Type.

    Therefore when you use

    Model.MacroParameters["imageSlide"]

    all you are able to get is the 'raw' ids of the images that have been picked in the multiple media picker.

    But you can turn these into IPublishedContent by using the UmbracoHelper's 'Media' method which accepts an array of Ids and returns an IEnumerable of IPublishedContent.

    So something like this might work for you:

    //you'll get a csv of the ids
    string pickedMediaCsv = Model.MacroParameters["imageSlide"].ToString();
    // you can then turn them into an array of ids
    string[] pickedMediaIds = String.Split(pickedMediaCsv, new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
    //now get the IPublishedContent representation
    IEnumerable<IPublishedContent> mediaItems = Umbraco.Media(pickedMediaIds);
    

    Hope that helps!

    regards

    Marc

  • Rich W 4 posts 84 karma points
    14 days ago
    Rich W
    0

    Marc,

    Thank you. I modified my code as follows:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web;
    @using Umbraco.Web.Composing;
    @using Clean.Core.Helpers;
    @{
    //you'll get a csv of the ids
    string pickedMediaCsv = Model.MacroParameters["sliderImage"].ToString();
    // you can then turn them into an array of ids
    string[] pickedMediaIds = String.Split(pickedMediaCsv, new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
    //now get the IPublishedContent representation
    IEnumerable<IPublishedContent> mediaItems = Umbraco.Media(pickedMediaIds);
    }
    @if (mediaItems != null)
    {
    <ul>
    @foreach (var item in mediaItems) {
    <li>Test</li>
    }
    </ul>
    }
    else{
    <p>nope</p>
    }
    

    I'm currently getting the following error:

    System.Web.HttpCompileException (0x80004005): C:\inetpub\wwwroot\UmbracoTemp\Views\MacroPartials\ImageSliderMacro.cshtml(10): error CS1503: Argument 1: cannot convert from 'string' to 'char'
    

    Any more help would be greatly appreciated.

  • Marc Goodson 1584 posts 10551 karma points MVP 6x c-trib
    1 week ago
    Marc Goodson
    100

    Sorry Rich

    string[] pickedMediaIds = pickedMediaCsv.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
    

    is this any better?

    regards

    Marc

  • Rich W 4 posts 84 karma points
    1 week ago
    Rich W
    0

    Marc,

    Yes sir, that did the trick.

    Thank you very much.

Please Sign in or register to post replies

Write your reply to:

Draft