Copied to clipboard

Flag this post as spam?

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


  • Nicky Christensen 76 posts 166 karma points
    Jan 23, 2012 @ 12:35
    Nicky Christensen
    0

    Simple check on field wont work

    Im trying to make a check on a field - I only want the HTML to output if the fields isn't empty - However, cant quite get it to work - What im doing is this? 

     

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
     
    if(string.IsNullOrEmpty(@Model.whiteBoxText.ToString())){
    <article class="other-jobs grey-gradient" >
        <div class="text">
          @Model.whiteBoxText
        </div>
        <div style="position:absolute; bottom:0; right:0;"><img src="@Model.Media("whiteBoxImage", "umbracoFile")" /></div>
    </article
    }
      }

  • Nicky Christensen 76 posts 166 karma points
    Jan 23, 2012 @ 12:39
    Nicky Christensen
    0

    Im i remove the ToString() i get an error saying:

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

  • Ben Norman 167 posts 276 karma points
    Jan 23, 2012 @ 12:48
    Ben Norman
    0

    Try updating the if to the following:

    if(Model.HasValue("WhiteBoxText"))
  • Nicky Christensen 76 posts 166 karma points
    Jan 23, 2012 @ 12:50
    Nicky Christensen
    0

    Then i get this error: 

    Error loading Razor Script RenderBoxJob.cshtml

    Cannot invoke a non-delegate type

     

    By the way, it is a Rich Text Editor field i want to make the check on, if that makes any difference? 

  • Ben Norman 167 posts 276 karma points
    Jan 23, 2012 @ 12:55
    Ben Norman
    0

    Isolate a test and rip everything else out except @Model.WhiteBoxText

  • Nicky Christensen 76 posts 166 karma points
    Jan 23, 2012 @ 12:59
    Nicky Christensen
    0

    If i remove everything, the field is being outputted... 

  • alimac 182 posts 371 karma points
    Jan 23, 2012 @ 13:07
    alimac
    0

    Doh, just realised it might be this:

    <div style="position:absolute; bottom:0; right:0;"><img src="@Model.Media("whiteBoxImage", "umbracoFile")" /></div>
    

    I think you have to change the quotes to:

    <div style="position:absolute; bottom:0; right:0;"><img src='@Model.Media("whiteBoxImage", "umbracoFile")' /></div>

  • Nicky Christensen 76 posts 166 karma points
    Jan 23, 2012 @ 13:11
    Nicky Christensen
    0

    Then it works :) :) :) 

    But any idea why it fails with HasValue() ?  

  • Nicky Christensen 76 posts 166 karma points
    Jan 23, 2012 @ 13:12
    Nicky Christensen
    0

    It works with the solution: 

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
     
    if(@Model.whiteBoxText != null &@Model.whiteBoxText.ToString(!= ""){
    <article class="other-jobs grey-gradient" >
        <div class="text">
          @Model.whiteBoxText
        </div>
        <div style="position:absolute; bottom:0; right:0;"><img src="@Model.Media("whiteBoxImage", "umbracoFile")" /></div>
    </article
    }
      }

  • alimac 182 posts 371 karma points
    Jan 23, 2012 @ 13:12
    alimac
    0

    Yea it's probably because it's case sensitive. You should be checking for "whiteBoxImage" instead of "WhiteBoxImage" I think?

    EDIT: oh my first solution worked? lol I deleted it thinking it was wrong, ha.

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jan 23, 2012 @ 13:36
    Michael Latouche
    0

    Hi Nicky,

    I think your test should be:

    if(!string.IsNullOrEmpty(@Model.whiteBoxText.ToString())){

    instead of

    if(string.IsNullOrEmpty(@Model.whiteBoxText.ToString())){

    If you want to write ou the HTMl on not empty nodes.

    Cheers,

    Michael.

  • alimac 182 posts 371 karma points
    Jan 23, 2012 @ 13:44
    alimac
    0

    ^ Ahhh, that's exactly it.. my silly eyes are failing me today!

  • Rodion Novoselov 694 posts 859 karma points
    Jan 23, 2012 @ 13:57
    Rodion Novoselov
    0

    Hi, actually when you write "ToString()" everything is casted to string. Otherwise despite the compiletime-type of the parameter you pass to the isNullOrEmpty() is dynamic the method "IsNullOrEmty" expects that the runtime-type of the parameter would be exactly the "string" type. So that if it's not a case then a runtime error is thrown.

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jan 23, 2012 @ 14:09
    Michael Latouche
    0

    Just for info: in .Net 4 you could replace the call to IsNullOrEmpty by a call to IsNullOrWhiteSpace. This does the same checks but also returns true if the string parameter consists of white spaces.

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft