Copied to clipboard

Flag this post as spam?

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


  • Andrew 6 posts 107 karma points
    Oct 20, 2016 @ 04:17
    Andrew
    0

    Cannot use 'Parse' as an argument to a dynamically dispatched operation - Multiple Media Picker

    Hi There,

    I am trying to get the first image from a multiple media selector control. I am doing this in a partial View that gets all the children of type "Listing" and displays a widget of the listing with the first image in the Images property .

    I built the partial view from one of the templates in umbraco and I am using version 7.5.3. There are no customisations and this is a fresh install.

    Any assistance will be greatly appreciated.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{  var selection = CurrentPage.Children("listing").Where("Visible"); }
    @if (selection.Any())
    {
        <ul>
            @foreach (var item in selection)
            {
                if (item.sold == false) 
                {
                if (item.HasValue("images"))
                {
                    var firstImageId = item.GetPropertyValue<string>("images").Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).FirstOrDefault();
                    var firstImage = Umbraco.TypedMedia(firstImageId);
    
                    if (firstImage != null)
                    {
                        <li class="col-md-4 col-sm-6 md-margin-bottom-30">
                            <div class="easy-block-v1">
                                <img class="img-responsive" src="@firstImage.Url" alt="listing image">
                                <div class="easy-block-v1-badge bg-color-justin">@item.Name</div>
                            </div>
                            <div class="block-grid-v2-info rounded-bottom">
                                <div class="row">
                                    <div class="col-sm-12 listing-icons">
                                        <p class="dp-inline-block margin-right-20"><i class="fa fa-bed"></i> 3</p>
                                        <p class="dp-inline-block margin-right-20"><i class="fa fa-bed"></i> 3</p>
                                        <p class="dp-inline-block margin-right-20"><i class="fa fa-bed"></i> 3</p>
                                        <p class="dp-inline-block"><i class="fa fa-bed"></i> 3</p>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-sm-12 text-left">
                                        <h3>@item.address</h3>
                                        <p class="dp-inline-block">
                                            <strong>Available</strong> @item.dateAvailable.ToString("")<br />
                                            <strong>Inspection</strong> @item.inspection
                                        </p>
                                        <a href="@item.Url" class="btn-u pull-right dp-inline-block">More...</a>
                                    </div>
                                </div>
                            </div>
                        </li>
                    }
                }
            }
        }
    </ul>
    

    }

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Oct 20, 2016 @ 20:48
    Alex Skrypnyk
    0

    Hi Andrew,

    Try to use this code:

    item.images.ToString()
    

    instaed of this one:

    item.GetPropertyValue<string>("images")
    

    item is dynamic type, I think this is the problem. Also we can rewrite all your view to strongly typed, what do you think is better for you?

    Thanks,

    Alex

  • Andrew 6 posts 107 karma points
    Oct 24, 2016 @ 03:38
    Andrew
    0

    Hey Alex,

    Thanks for getting back to me. I tried this and its still giving the same error. To try and debug the problem i tried to do the tostring() on just the images and it says that its already a string. and the output looks like

    1086,1087,1088,1089,1090,1091,1092,1093,1094,1095

    I even just tried taking out the select statement and getting the FirstOrDefault item from the string array and its telling me that "System.Array" does not contain a definition for FirstOrDefault.

    By the look of it its not liking Linq functions. I even tried adding using System.Linq to the page and it still did nothing.

    Do you have any idea what the issue could be. hopefully yhe above will help narrow things down.

    CHeers Andrew

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 03, 2016 @ 22:34
    Alex Skrypnyk
    0

    Hi Andrew,

    It's really strange issue, what version of Umbraco are you using?

    This is working for me:

    var nodeIds = "1086,1087,1088,1089,1090,1091,1092,1093,1094,1095".Split(',').Select(int.Parse);
    

    Thanks,

    Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 08, 2016 @ 22:54
    Alex Skrypnyk
    0

    Hi Andrew,

    Di you find a solution?

    Can you share with community?

    Thanks,

    Alex

  • Andrew 6 posts 107 karma points
    Apr 10, 2017 @ 11:22
    Andrew
    101

    Apologies for the delay in posting the solution.

    The solution was to get the images and post them to an array and the grab the first item in the array.

    Split the Multiple media collection

    var Images = item.images.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    var ImagesArray = Umbraco.Media(images);
    

    And then read the first item of the array.

    <img class="img-responsive" src="@ImagesArray[0].Url" alt="listing image">
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 10, 2017 @ 13:51
    Alex Skrypnyk
    1

    Hi Andrew

    Thank you for sharing!!!

    Have a nice day

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft