Copied to clipboard

Flag this post as spam?

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


  • Rylan 67 posts 251 karma points
    Jun 23, 2017 @ 18:03
    Rylan
    0

    Contentpicker2 | Umbraco 7.6.3 - Grab all items

    Is it possible to grab more than one item? I can't seem to figure it out, I just got it to pull a single image, which to me doesn't make too much sense.

    Code

      @{
                        var typedMediaPickerSingle = Model.Content.GetPropertyValue<IPublishedContent>("pageGrab");
                        if (typedMediaPickerSingle != null)
                        {
                            <img src="@typedMediaPickerSingle.Url" style="width:200px" alt="@typedMediaPickerSingle.GetPropertyValue("alt")" />
                        }
                    }  
    

    It's kind of similar to related items that will show the image, name, and description.

    Thanks for your time.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 23, 2017 @ 20:34
    Alex Skrypnyk
    2

    Hi Rylan

    If you use multiple media picker and want to render it, use this code:

    var typedMediaPickerItems = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("pageGrab");
    

    Thanks,

    Alex

  • Rylan 67 posts 251 karma points
    Jun 27, 2017 @ 22:54
    Rylan
    0

    Thanks for the reply Alex.

    I could get the loop working for @item.name, but couldn't get a image to be pulled dynamically from the content. Any suggestions?

    @{
        var typedMediaPickerItems = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("pageGrab");
    }
        @foreach (var item in typedMediaPickerItems){
                   <div class="col-sm-4 text-center">
                       @if(item.HasValue("mainImage")){
                            var mainImg = item.mainImage;
                            <img src="@mainImg.Url" alt="@mainImg.Name" />
                        }
                       <p>@item.Name</p>
                   </div>
                }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 28, 2017 @ 07:52
    Alex Skrypnyk
    0

    Hi Rylan

    Try this code:

            var mainImg = item.GetPropertyValue<IPublishedContent>("mainImage");
            <img src="@mainImg.Url" alt="@mainImg.Name" />
    
  • Rylan 67 posts 251 karma points
    Jun 28, 2017 @ 15:00
    Rylan
    0

    enter image description here

    If I move the variable down: enter image description here

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 29, 2017 @ 10:12
    Alex Skrypnyk
    0

    Rylan, try this code:

    @{
        var typedMediaPickerItems = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("pageGrab");
    }
    @foreach (var item in typedMediaPickerItems)
    {
        <div class="col-sm-4 text-center">
            @{
                var mainImg = item.GetPropertyValue<IPublishedContent>("mainImage");
            }
            @if (mainImg != null)
            {
                <img src="@mainImg.Url" alt="@mainImg.Name" />
            }
            <p>@item.Name</p>
        </div>
    }
    
  • Rylan 67 posts 251 karma points
    Jun 30, 2017 @ 15:27
    Rylan
    0

    No Luck Alex. I'm building this on the web platform. I am not using Visual Studio.

    enter image description here

    here is all my code.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "BasePage.cshtml";
        @*var typedMediaPickerItems = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("pageGrab");*@
    }
    <div class="main">
        <div class="container">
            <div class="row">
                <div class="col-sm-12">
                    @* CURRENT PAGE MAIN IMAGE *@
                    @if(CurrentPage.HasValue("mainImage")){
                        var pgImg = CurrentPage.mainImage;
                        <img src="@pgImg.Url" alt="pgImg.Name" />
                    }
                    <h2>@CurrentPage.contentTitle</h2>
                    @CurrentPage.content
                </div>
            </div>
            <div class="row">
                @*
                   @foreach (var item in typedMediaPickerItems){
                   <div class="col-sm-4 text-center">
                        var mainImg = item.GetPropertyValue<IPublishedContent>("mainImage");
                        <img src="@mainImg.Url" alt="@mainImg.Name" />
                       <a href="@item.Url">@item.Name</a>
                   </div>
                }
                *@
                @{
                    var typedMediaPickerItems = Model.GetPropertyValue>("pageGrab");
                }
                @foreach (var item in typedMediaPickerItems){
                    var mainImg = item.GetPropertyValue("mainImage");
                    if(mainImg != null){
                        <img src="@mainImg.Url" alt="@mainImg.Name" />
                    }
                    @item.Name
                }
            </div>
        </div>
    </div>
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 30, 2017 @ 17:06
    Nik
    0

    Hi Rylan,

    You've got a stray > on this line here:

     var typedMediaPickerItems = Model.GetPropertyValue>("pageGrab");
    

    Try:

     var typedMediaPickerItems = Model.GetPropertyValue("pageGrab");
    
  • Rylan 67 posts 251 karma points
    Jun 30, 2017 @ 17:11
    Rylan
    0

    Yeah thought it was intentional. I removed it after I saw it but still didn't work!

    enter image description here

  • Mark Bowser 273 posts 860 karma points c-trib
    Jun 30, 2017 @ 17:42
    Mark Bowser
    0

    Try saying Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("pageGrab");

    If the Model is a RenderModel, the Model.Content will give you an IPublishedContent usually representing the current page in umbraco unless you've done some route hijacking or something interesting like that. The IPublishedContent should have the .Url, .Name, and .GetPropertyValue(...) on it.

  • Rylan 67 posts 251 karma points
    Jun 30, 2017 @ 17:51
    Rylan
    101

    Thanks Mark, that gave me more insight and it worked!

    Here is the solution:

     @{
                var typedMediaPickerItems = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("pageGrab");
            }
            @foreach (var item in typedMediaPickerItems){
                <div class="col-sm-4">
                    @if(item.HasValue("mainImage")){
                        var mainImg = item.GetPropertyValue<IPublishedContent>("mainImage");
                        <img src="@mainImg.Url" alt="mainImg.Name" />
                    }
                    @item.Name
                </div>
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft