Copied to clipboard

Flag this post as spam?

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


  • Karen 8 posts 98 karma points
    Jan 29, 2018 @ 12:04
    Karen
    0

    Hi, I'm not a coder, I'm a designer - I copy and paste code in from other sites and then try and tweak to make it work!

    And I can't get the related links bit to work.

    I have a partial which is linked to from the homepage of the site: -

    @{
            IEnumerable<IPublishedContent> adverts = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("advertLinks");
            @Html.Partial("PRSP/PRSP-NestedContent/PRSP-AdvertLinks", adverts);
        }
    

    and I have a partial which is: -

    @using Umbraco.Web.Models;
    
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<IPublishedContent>>
    
        <div class="row add-lg-marg-tb">
    
            @{
                foreach (IPublishedContent advert in Model.Where(x => x.IsVisible()))
    
                {
                    string advertTitle = advert.GetPropertyValue<string>("title");
                    string advertDesc = advert.GetPropertyValue<string>("description");
                    string imageUrl = advert.HasValue("image") ? advert.GetPropertyValue<IPublishedContent>("image").Url : "media/1016/advert-crimestoppers-newlogo.jpg";
                    string altText = "Descriptive image";
    
                    //look for the type RelatedLink within imageLinks and refer to them as links
                    IEnumerable<RelatedLink> links = advert.GetPropertyValue<IEnumerable<RelatedLink>>("imageLink");
                    //if RelatedLink has a reference link and is not empty, if is empty leave blank (return null)
                    RelatedLink link = links != null ? links.FirstOrDefault() : null;
    
                     <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 add-lg-marg-b">
                         <div class="card">
                            <img class="card-img-top" src="@imageUrl" alt="@altText" data-holder-rendered="true">
                            <div class="card-body">
                                <h3 class="card-title">@advertTitle</h3>
                                <p>@advertDesc</p>
                                <p><a class="btn btn-primary btn-lg head-button noIcon-only" role="button" target="@(link.NewWindow ? "_self" : null)" href="@link.Link" href="#">@link.Caption</a></p>
                            </div>
                        </div>  
                    </div>
                }
            }
        </div>
    

    The error I get is this:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    And the stack line reference is:

    <p>@advertDesc</p>
    

    When I comment out the p tag where I would like the link to appear all is displayed as it should be apart form the link that I commented out.

    I really don't understand. When I comment out all code apart from the link code it returns an error:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    and it complains about:

    RelatedLink link = links != null ? links.FirstOrDefault() : null;
    

    And if I comment that line out as I think it just checks to make sure there is a link the error message I get is related to the p tag and it says 'link' does not exist.

    I've used the same code on a Carousal from this tutorial (thank you brillaint :)) and it worked fine in that.

    sorry for the long post but I find it very helpful for all references shown when I read other's posts.

    Thank you for taking a look - learning something new every day.

  • Alex Skrypnyk 6134 posts 23953 karma points MVP 8x admin c-trib
    Jan 29, 2018 @ 13:02
    Alex Skrypnyk
    1

    Hi Karen

    Add please a check for a null like that:

            RelatedLink link = links != null ? links.FirstOrDefault() : null;
    
            if (link != null)
            {
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 add-lg-marg-b">
                    <div class="card">
                        <img class="card-img-top" src="@imageUrl" alt="@altText" data-holder-rendered="true">
                        <div class="card-body">
                            <h3 class="card-title">@advertTitle</h3>
                            <p>@advertDesc</p>
                            <p><a class="btn btn-primary btn-lg head-button noIcon-only" role="button" target="@(link.NewWindow ? "_self" : null)" href="@link.Link" href="#">@link.Caption</a>
                            </p>
                        </div>
                    </div>
                </div>
            }
    
  • Karen 8 posts 98 karma points
    Jan 29, 2018 @ 14:20
    Karen
    100

    Thank you Alex. That was it.

    I was missing the logic that only one content container had all its content in, including a link. The other containers were empty which is why they showed the default image when I commented out the link code, and not an error message.

    The link container needs checking because it could have more than one link in it, because it is a list element or an IEnumerable, so the code checks can't find any links for container two and throws a null error.

    I could make the links content document type mandatory or add an else statement to say no link don't display p tag, which is probably the best thing to do.

    Thanks again I have updated my code to:

    @using Umbraco.Web.Models;
    

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<>

    <div class="row add-lg-marg-tb">
    
        @{
            foreach (IPublishedContent advert in Model.Where(x => x.IsVisible()))
    
            {
                string advertTitle = advert.GetPropertyValue<string>("title");
                string advertDesc = advert.GetPropertyValue<string>("description");
                string imageUrl = advert.HasValue("image") ? advert.GetPropertyValue<IPublishedContent>("image").Url : "media/1016/advert-crimestoppers-newlogo.jpg";
                string altText = "Descriptive image";
    
                //look for the type RelatedLink within imageLinks and refer to them as links
                IEnumerable<RelatedLink> links = advert.GetPropertyValue<IEnumerable<RelatedLink>>("imageLink");
                if (links != null)
                {
                    //if RelatedLink has a reference link and is not empty, if is empty leave blank (return null)
                    RelatedLink link = links.FirstOrDefault();
                    if (link != null)
                    {
                        <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 add-lg-marg-b">
                            <div class="card">
                                <img class="card-img-top" src="@imageUrl" alt="@altText" data-holder-rendered="true">
                                <div class="card-body">
                                    <h3 class="card-title">@advertTitle</h3>
                                    <p>@advertDesc</p>
                                    <p><a class="btn btn-primary btn-lg head-button noIcon-only" role="button" target="@(link.NewWindow ? "_self" : null)" href="@link.Link" href="#">@link.Caption</a></p>
                                </div>
                            </div>  
                        </div>
                    }
    
                }
                else
                {
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 add-lg-marg-b">
                            <div class="card">
                                    <img class="card-img-top" src="@imageUrl" alt="@altText" data-holder-rendered="true">
                                    <div class="card-body">
                                        <h3 class="card-title">@advertTitle</h3>
                                        <p>@advertDesc</p>
    
                                   </div>
                             </div> 
                        </div>
                }
            }
        }
    </div>
    

    Thank you :)

  • Alex Skrypnyk 6134 posts 23953 karma points MVP 8x admin c-trib
    Jan 29, 2018 @ 14:47
    Alex Skrypnyk
    0

    You are welcome, Karen

    Ask if you have a questions

Please Sign in or register to post replies

Write your reply to:

Draft