Copied to clipboard

Flag this post as spam?

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


  • Hefin Jones 39 posts 161 karma points
    Jul 05, 2012 @ 14:38
    Hefin Jones
    0

    .HasValue doesn't seem to work

    Hi Guys!

    We have setup a media type called "Banner" this has the same properties as a media image, except it has an additional tab with containts a field ("imageLink") to hold a URL Link value which is a textfield. On our document type we have an Ultimate picker field which is called "bannerItems" where a user can select multiple items from the Banners folder in the media section.

    I then wrote some razor to output the selected banners onto the relevant page. The razor code is as follows:

    @if(Model.HasValue("bannerItems"))
    {
    <div id="banners">
    @foreach(var nodeId in Model.GetProperty("bannerItems").Value.Split(','))
    {
    var banner = Library.NodeById(nodeId);
    if (banner.HasValue("imageLink"))
    {
    <a href="@banner.imageLink" target="_blank"><img src="@banner.umbracoFile" class="" /></a>
    }
    else
    {
    <img src="@banner.umbracoFile" class="" />
    }
    }
    </div>
    }

    Quite a simple script which checks to see if the imageLink field has a value stored in it and then renders the relevent banner with or without a link.  But I can't seem to get the .HasValue to work it always renders the banners without the link.  And when I output the value of imageLink to the page like this:

    <p>@banner.imageLink</p>

    I get the following results:

    For banners that have text stored in the field I get the value as expected (e.g. http://www.google.com), but for banners that have no value against imageLink I get the following outputted to the screen:

    umbraco.MacroEngines.DynamicXml

    Any help on this would be greatly appreciated.

    Cheers,

    Hefin 

     

  • gilad 185 posts 425 karma points
    Jul 05, 2012 @ 14:42
    gilad
    0

    Hii Hefin

    try this : 

    if(banner.imageLink.ToString()!="")

     

  • Hefin Jones 39 posts 161 karma points
    Jul 05, 2012 @ 14:57
    Hefin Jones
    0

    Thanks for your reply, unfortunatley I still get the same behaviour.  After adding your code the result I get is as follows:

     

    <div id="banners">
    <a href="http://www.link.com/" target="_blank"><img src="/media/3007/half-marathon.gif" class="" /></a>
    <a href="umbraco.MacroEngines.DynamicXml" target="_blank"><img src="/media/3023/festivalsmalleng.gif" class="" /></a>
    </div>

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 05, 2012 @ 15:12
    Fuji Kusaka
    0

    Hi Hefin, 

    You could check if the upload file is empty 

     

    @{
          if(String.IsNullOrEmpty(@Model.imageLink.ToString()) == false){
            <img src="@Model.imageLink"/>
            }   
          else{
               @:do something else
               }
      }

     

    //fuji

     

  • Hefin Jones 39 posts 161 karma points
    Jul 05, 2012 @ 15:41
    Hefin Jones
    0

    That gives the same result as above.  

    <divid="banners">
    <a href="http://www.link.com/"target="_blank"><imgsrc="/media/3007/half-marathon.gif"class=""/></a>
    <a href="umbraco.MacroEngines.DynamicXml"target="_blank"><imgsrc="/media/3023/festivalsmalleng.gif"class=""/></a>
    </div>

    Where as the result should be:

    <divid="banners">
    <a href="http://www.link.com/"target="_blank"><imgsrc="/media/3007/half-marathon.gif"class=""/></a>
    <img src="/media/3023/festivalsmalleng.gif"class=""/>
    </div>

    Where the link isn't rendered if the imageLink field is left blank.  But each check / if statement doesn't seem to work or it thinks it contains "umbraco.MacroEngines.DynamicXml"

  • gilad 185 posts 425 karma points
    Jul 06, 2012 @ 14:25
    gilad
    0

    Try this :

     

    if (!String.IsNullOrEmpty(bannr.getProperty("imageLink").Value.ToString()))

     

  • Tony Kiernan 278 posts 341 karma points
    Jul 09, 2012 @ 14:29
    Tony Kiernan
    0

    Something like:

    var banners = Model.bannerItems;
    if(banners.Count() > 0)
    {
    }

    I've had similar issues when I've added a propertry to a doctype when there are existing pages of that doctype. 

  • 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