Copied to clipboard

Flag this post as spam?

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


  • Ben Schlaepfer 74 posts 101 karma points
    Dec 12, 2013 @ 18:43
    Ben Schlaepfer
    0

    Razor - "simple" inline if statement causes MacroEngine error

    Hi there.

    I have a simple inline if statement in my template that tests for a field value then displays appropriate html.

    The code is as follows:

              @if (Model.HasValue("dL3strippingDims"))
    
        { 
          var family = Model.connectorFamily;
          var dl3 = Model.Media("dL3strippingDims");
    
            <tr>
                    @if (family="Inter Series Adaptor")
    
                    { 
                <td width="20%">Stripping Dimensions</td> 
                    } 
                    else 
                    { 
                <td width="20%">Int Dims 2</td> 
                    }
    
              <td width="80%"><a href="@dl3.umbracoFile" target="_blank">@dl3.Name</a></td>
            </tr>
       }   
    

    I thought I knew how to do this, but I guess my razor syntax is awry - I get the error message

    Error loading MacroEngine script (file: )
    

    (If I just display the value of the var "family" it works fine and displays "Inter Series Adaptor" as I would expect.

    Any ideas or guidance gratefully received.

    Thanks Ben

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 12, 2013 @ 20:00
    Dennis Aaen
    100

    Hi Ben,

    I have just tried to work with your code, and I think I found a solution for you.

    The thing that gives you the error is your if statement where you try to check if the variable called family is equal to Inter Series Adaptor

    Try this code instead:

        @if (Model.HasValue("dL3strippingDims")){ 
          var family = Model.connectorFamily;
          var dl3 = Model.Media("dL3strippingDims");

            <tr>  @if (family == "Inter Series Adaptor")

                    {
                <td width="20%">Stripping Dimensions</td>
                    }
                    else
                    {
                <td width="20%">Int Dims 2</td>
                    }

                 
              <td width="80%"><a href="@dl3.umbracoFile" target="_blank">@dl3.Name</a></td>
            </tr>
       }  

    When you try to check if a variable has a particular string value, then use two equal signs.

    I hope it helps,

    /Dennis

  • Ben Schlaepfer 74 posts 101 karma points
    Dec 13, 2013 @ 09:45
    Ben Schlaepfer
    0

    Hey Dennis,

    Works a charm! Can't believe I missed that.

    Thanks very much.

    Ben

Please Sign in or register to post replies

Write your reply to:

Draft