Copied to clipboard

Flag this post as spam?

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


  • Nicolai Winch Kristensen 50 posts 70 karma points
    May 14, 2013 @ 15:50
    Nicolai Winch Kristensen
    1

    HasValue, HasProperty checking Content Picker with razor problem

    Hi there 

    I really often run into problems when try to do conditional checks (if the property have value) on different umbraco properties, when using razors.

    fx:  a slideshow. - I  added a generic property 'link' as a 'content picker' to the standard umbraco image doctype. 

    Now I want to check if the link is choosen by editor.
    If so: let the image in slideshow link.
    If not: then just ad the image.

    tried different things fx

     

    <ul>

    @foreach (dynamic media in mediaItem.Children.Items)

    {

    <li>

               @if(media.HasValue("link")){               

                  dynamic anchorNode = new DynamicNode(media.link);

                      <a class="images" href="@anchorNode.Url">

                          <imgsrc="@media.umbracoFile"width="@media.umbracoWidth height="@media.umbracoHeight" alt="@media.Name" />

                      </a>

     

                 }else{

            <img src="@media.umbracoFile" width="@media.umbracoWidth" height="@media.umbracoHeight" alt="@media.Name" />

                }

    </li>

    }

    </ul>

     

    for some reason the media.HasValue("link")  (or also if using HasProperty) always returns true.

     

    I also tried to write out the value. if theres a value in the content picker it returns the ID of the referenced node (which is OK). 

     

    But.. if I delete the the reference in the content picker it returns 'umbraco.MacroEngines.DynamicXml'.??

    If this is treated as positive result for the HasVAlue or HasProperty, its method is not of much use is it? or am I totally misunderstanding something here?

     

    can anyone help me with this? :-)

     

    best regards :-)

    Nicolai

     

  • Fuji Kusaka 2203 posts 4220 karma points
    May 14, 2013 @ 15:58
    Fuji Kusaka
    0

    Hi Nicolai

    What do you get if you change it to something like this 

    @if(!String.IsNullOrEmpty(media.contentPicker)){
    <a href="@Model.NodeById(media.contentPicker).NiceUrl">@media.Name</a>
    }
  • Nicolai Winch Kristensen 50 posts 70 karma points
    May 14, 2013 @ 21:52
    Nicolai Winch Kristensen
    0

    thx for taking time to answer

    if use as suggested:  @if(!String.IsNullOrEmpty(media.contentPicker)){

    then I get the error

    Error loading Razor Script Slideshow.cshtml
    The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments

     

    I then tried to convert it to a string (guess error is related to the fact that the contentPicker returns an int)

    @if(!String.IsNullOrEmpty(media.contentPicker.ToString())){

    now the error is not thrown, but thre condtion is still 'true' even no content in contentPicker.
    I guess it now returns ID as string or 
    umbraco.MacroEngines.DynamicXml

    Which again is a positive statement 

  • Moran 285 posts 934 karma points
    May 14, 2013 @ 22:37
    Moran
    0

    Try to use this:

    DynamicNode d = Model;
    if(!string.isNullOrEmpty(d.GetPropertyValue("contentPicker")))

    I think this should work, I built  function with that code that wiil always check to see if my property is empty.

  • Charles Afford 1163 posts 1709 karma points
    May 14, 2013 @ 22:44
    Charles Afford
    0

    Moran, i would say that one look right.  Althought i would do:

    DynamicNode ctxnode = new DynamicNode(@Model.context.Current.Id) (i think thats right)

     

    Nichola, you always want to check wether the property exsist with:

     

     node.HasProperty("")

    Hope this helps :) 

  • Nicolai Winch Kristensen 50 posts 70 karma points
    May 14, 2013 @ 22:49
    Nicolai Winch Kristensen
    0

    This actually solved it:

    @if(     @media.link.GetType().ToString(== "System.Int32"  ){

     

    not beautifull, but it works. Gues the main problem is that it returns an int if
    content picker is choosen but a string if its empty.

    So above converts it to a string , and then do the check on the string.

    @Moran I will try and check you solution to tommorow morning

     

    thx :-) Nicolai

     


  • Nicolai Winch Kristensen 50 posts 70 karma points
    May 14, 2013 @ 22:51
    Nicolai Winch Kristensen
    0

    @Charles my problem is that HasProperty returns  true in both cases. - it returns umbraco.MacroEngines.DynamicXml  If content picker is empty.  

  • Moran 285 posts 934 karma points
    May 15, 2013 @ 10:29
    Moran
    0

    You're welcome :) 

Please Sign in or register to post replies

Write your reply to:

Draft