Copied to clipboard

Flag this post as spam?

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


  • Joseph 12 posts 61 karma points
    Apr 07, 2015 @ 12:19
    Joseph
    0

    Accessing all images for a doctype in Razor

    Hi.  I'm finding Umbraco extremely difficult to use.  I'm very new (my first post).  I'm trying to create a new page to simply display a bunch of uploaded images in a grid like manner.

    I created two docTypes, one called Testimonial and another called Testimonials - Testimonials is under the base doctype.

    In Testimonials, I set the allowed template and default template to Testimonials.  Under structure - Allowed child nodetypes, I only checked Testimonial.

    In the Testimonial doctype, I added a Tab called Content.  Within that content tab, I added an Image picker field.

    My question is, how do I display all the images for every Testimonial onto the Testimonials Razor view?  Is there a way to generate all the Razor?  If not, where would the info be on how to loop all the images for each Testimonial.

    Many thanks.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Apr 07, 2015 @ 13:45
    Ismail Mayat
    0

    Joseph,

    Take a look at https://our.umbraco.org/documentation/Reference/Templating/Mvc/partial-views so in your Testimonials view you would do something like

    @foreach(var page in Model.Content.Children.Where(x => x.IsVisible())){
    
    }

    in the loop for each page which is this case will be of type Testimonial you would get the image property and then get the media item. So something like

    var image = Umbraco.TypedMedia(page.GetPropertyValue<string>("yourimagefieldaliashere"))

    then <img src="@image.Url"/>

    Something along those lines.

    Regards

    Ismail

     

  • Joseph 12 posts 61 karma points
    Apr 07, 2015 @ 16:03
    Joseph
    0

    Hi Ismail,

    Thanks for getting back to me.  It's making a lot more sense now. I tried the following:

    @for (int i = 0; i < Model.Content.Children.Count(); i++)
    {
        var page = Model.Content.Children.ElementAt(i);
       
        if (page.IsVisible())
        {
            var image = Umbraco.TypedMedia(page.GetPropertyValue("companyImage")); << Object not set to instance of object
           
           
        }
    }

    Any ideas why I'm getting a null ref exception?  I have images present and published.

    Many thanks,

    Joseph.

     

  • Joseph 12 posts 61 karma points
    Apr 08, 2015 @ 12:36
    Joseph
    100

    I solved it with:

    @for (int i = 0; i < Model.Content.Children.Count(); i++)
    {
        var page = Model.Content.Children.ElementAt(i);

        if (page.IsVisible() && page.HasValue("companyImage"))
        {
            dynamic prop = page.GetPropertyValue("companyImage");       
            <img src="@prop[0].Url" />
        }
    }

Please Sign in or register to post replies

Write your reply to:

Draft