Copied to clipboard

Flag this post as spam?

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


  • Ryan 35 posts 129 karma points
    Jun 26, 2012 @ 17:46
    Ryan
    0

    Razor If Statements

    Hi, I'm new to Umbraco and Razor.  I'm testing with Umbraco 4.7.2.

    I'm trying to see if a field has a value and checkbox is checked.  "myLink" is a textstring and "newWindow" is a true/false.  Basically, I'm trying to see if the link should be opened in a new window.  I know you can do this easily in the WYSIWYG editor, however, I'm trying to build some custom functionality for an image rotator.

    <umbraco:Macro runat="server" language="cshtml">
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    if (Model.HasValue("myLink") && Model.newWindow == 1){
    <a href="@Model.myLink" target="_blank">@Model.myLink</a>
    }else if(Model.HasValue("myLink")){
    <a href="@Model.myLink"">@Model.myLink</a>
    }
    }
    </umbraco:Macro>

    While I'm asking, if this the best way to check if a field has text?

    if (Model.HasValue("someField")){@Model.someField}

    Thanks your input!

  • Barry Fogarty 493 posts 1129 karma points
    Jun 26, 2012 @ 19:00
    Barry Fogarty
    0

    Hi Ryan, welcome to the forum!

    Yes as far as I know @Model.HasValue("someField") is the recommended way in Razor to check for a value in a field, as it will also handle a null reference too (if the field is not found).

    Re your if statement, you could try something like this:

    @{
    var linkTarget = (Model.newWindow == 1) ? " target=\"_blank\"" : "";
    <a href="@Model.myLink"@Html.Raw(linkTarget)>@Model.myLink</a>
    }

    Good luck!

     

     

  • Ryan 35 posts 129 karma points
    Jun 26, 2012 @ 19:16
    Ryan
    0

    Thanks for the reply Barry!  I'm still having some trouble.  I've tested the true/false field and have seen both "0/1" or "False/True" as the value.  Currently, its showing as "True"  I tried to modify your code to use "True" but it's throwing an error.  Any suggestions?  I really appreciate the help!

    @{
       
    var linkTarget =(Model.newWindow =="True")?" target=\"_blank\"":"";
       
    <a href="@Model.myLink"@linkTarget>@Model.myLink</a>
    }
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 26, 2012 @ 22:03
    Dan Diplo
    0

    A true/false field should convert to an actual boolean, so you can do:

    var linkTarget = (Model.newWindow) ? " target=\"_blank\"" : String.Empty;
  • Ryan 35 posts 129 karma points
    Jun 26, 2012 @ 22:41
    Ryan
    0

    Thank you Dan!  That worked.

    I did have another if statement question.  How do you check if a media picker field has a value?

    Thanks again.  The community here is great!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 27, 2012 @ 09:28
    Dan Diplo
    1

    In 4.7.2 probably the safest way is to use

    if (Model.HasValue("YourPropertyAlias")) { }

    You can also do

    if (!String.IsNullOrEmpty(Model.YourPropertyAlias.ToString()) { }
Please Sign in or register to post replies

Write your reply to:

Draft