Copied to clipboard

Flag this post as spam?

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


  • Naveed Ali 161 posts 426 karma points
    May 13, 2016 @ 10:53
    Naveed Ali
    0

    check dynamic item

    Hi,

    I am trying to check if a dynamic item is null or not.

    I have used the "HasValue" as recommended in my Google searches but it does not work. Using this just make it skip this block of code

    here is my code snippet. I am trying to check if linkToPage has a value.

      @{
            if (CurrentPage.HasValue("featuredItems"))
    
            {
                var featureList = CurrentPage.featuredItems.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                var featureCollection = Umbraco.Content(featureList);
    
    
    
                foreach (var item in featureCollection)
                {
                    <div class="col-sm-4 col-xs-6 margin30 featured-content-block">
                        <div>
                          @if (item.HasValue(@item.linkToPage))
                          {
                            <a href="/@item.linkToPage/">
                                <div class="item-img-wrap">
                                    <img src="@Umbraco.Media(@item.featureImage).Url" class="img-responsive" alt="">
                                </div>
                            </a>
                          }
                          else
                          {
                            <div class="item-img-wrap">
                                <img src="@Umbraco.Media(@item.featureImage).Url" class="img-responsive" alt="">
                            </div>
                          }
    
    
                            <div class="news-desc">
                                <h4 style="color:white">@item.featureText</h4>
                            </div>
                        </div>
                    </div>
                }
            }
        }
    
  • Naveed Ali 161 posts 426 karma points
    May 13, 2016 @ 10:58
    Naveed Ali
    1

    I found the answer.. for anyone else having this issue.. you need to write the alias name of the property so my code from above has changed to this:

     @if (@item.HasValue("linkToPage"))
    

    So it is if item has a value in there called link to page then do whatever is within the block of code.

    Thanks

    Nav

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies