Copied to clipboard

Flag this post as spam?

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


  • Gurjot Singh 5 posts 35 karma points
    Oct 24, 2014 @ 19:33
    Gurjot Singh
    0

    Best method to display all images using a multi-media picker

    Hi,

    I have added a multiple media picker to my content page.  I can access all the ids for the items selected using @Umbraco.Field("strainImage") and all that returns me a list of ids.  What would be the best method to retrieve the items which I can loop through? I would like to get the URLS preferably in a method to loop through.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 25, 2014 @ 13:44
    Dennis Aaen
    0

    Hi Gurjot,

    What you need is to create a partal view or a partial view macro then you can loop through all the images that has been picked in the multi-media picker. You can choose the write dynamic razor or strongly typed razor, both version will works whatever you choose to use partal view or a partal view macro.

    In my example I have choosen to create a partial view macro, and choose the dynmaic razor version. With this code you will get the images that are selected on the page that you are viewing in the browser.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @if (CurrentPage.HasValue("strainimage"))
    {
        var caseStudyImagesList = CurrentPage.strainimage.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);

        foreach (var caseStudyImage in caseStudyImagesCollection)
        {
            <img src="@caseStudyImage.Url" style="width:300px;height:300px" />
        }
    }

    If you are not familiar with how to create a partial view or partial view marco here a some documentation on how to create a parital view macro http://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/Articles-Parent-and-Article-Items

    If you rather want to use partial views, then go to the settings section into the folder partial views, and create a new file give it a name, and past in the code above, as I said before you can use the code in both situations whatever if you choose to use partal view or a partal view macro.

    The difference between the partial view and a partial view macro is where it @inherits from, the partial view are inherit from:

    @inherits UmbracoTemplatePage

    Where the parial view macro are inherit from:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    When you have created your razor file and pasted in the code, you need to add the file into your template where you need the images to display.

    If you choose the partial view macro you need to add this to the template

    @Umbraco.RenderMacro("Mediapicker") 

    In my example I have named my razor file Mediapicker. If you see the documentation I have linked to higher up it will also explain it. For the partial view you need to add this into your template where the images should display:

    @{Html.RenderPartial("Mediapicker"); }

    Again I have choosen to call my partial view file media picker. As you probably already has discovered Umbraco comes with some pre-defined data types, including the multi-media picker, try to see this documentation about the property editors in Umbraco 7 http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/.

    As you can see there a some documentation http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors-v7/Media-Picker for the media picker and how to pull out the images, and there are shown both razor types that I described eailer in this post. The dynmaic one and the strongly typed razor.

    Hope this helps and make sense,

    /Dennis

  • Craig100 1136 posts 2523 karma points c-trib
    Feb 13, 2015 @ 17:27
    Craig100
    0

    Hi Dennis,

    In a V7.2.1 site, using your example I just get :  'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Split'

    Here's the code:-

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var productList = CurrentPage.Site().FirstChild("ProductList").Children("ProductItem").Where("Visible");
    
        foreach(var product in productList) {
        var skuList = product.Children.Where("Visible && DocumentTypeAlias == @0","Sku");
        <section class="productpage-section">          
            <div class="row features-boxes product-border">
                <div class="col-l-3 col-m-3 col-s-3">
                <div class="productimg-lft ortac">                    
                @foreach (var sku in skuList) {
                    if (sku.HasValue("productImages")) {
                        var imageList = sku.productImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            var productImage = Umbraco.Media(imageList[0]);
                            <img src="@productImage.GetResponsiveCropUrl("ProductUSP")" alt="Test">
                            }
                        }                   
                    </div>
                </div>
    </div>
    </section> 

    Any clue to what's missing/happening would be appreciated. Just worried it's this issue: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/53022-UmbracoWebModelsDynamicPublishedContentList-does-not-contain-a-definition-for-Any   Hopefully not.

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft