Developer forum

Forum » Templates » Trying to understand Razor...

Trying to understand Razor...

Lise T. Pedersen
Reply

I have translated a hmtl to a racor template, and is now trying to "rebuild" the if-sentences in my template.

But I have this, starting in line 6:

 

             @if (GetValue("ParagraphContainerSort") > "0") {
              <div class="product__body" style="display:block;">
              } 
              else {
              <div class="product__body">
              }
             }

 

and I get the error

Line 6: The if block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block,

And I have too little experience to see what's wrong.

If I compare to the examples from "From Dynamicweb HTML to Razor" on Github, I think it looks "the same". Need a sharp eye on it...

Thanks?

Lise


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Lise

Yes, it is a new world :-).

You have to make sure you have the same amount of { and } - and if you count, you have a } too many. Another issue is that you have an integer check and not a string check, so your code should look like this:

@if(GetInteger("ParagraphContainerSort") > 0) {
              <div class="product__body" style="display:block;">
}
 else {
              <div class="product__body">
}

 
Nicolai Høeg Pedersen
Reply

By the way, this might be a better approach that is easier to read:

 

@{
    var productBodyStyle = "";
    if (GetInteger("ParagraphContainerSort") > 0)
    {
        productBodyStyle = "display:block;";
    }
}

<div class="product__body" style="@productBodyStyle;">

...

</div>

 
Lise T. Pedersen
Reply

It helped - thanks.

But my next challenge is this loop - I hope you can help me again.

I have created an item where the product is available in different colours, and these colours is defined in a checkbox list - and if the colour is checked, it shall be in the loop.

Until now, the "automatic translation" to Razor has given me this code:

 

                      <ul>
                        @foreach (var item in GetLoop("Item.Farvemuligheder.Options")) {
                         
                          if(GetBoolean("Item.Farvemuligheder.Option.IsSelected") != null) {
                        <li style="text-align: left; width: 99px; height: 61px;"><img src="/Files/Billeder/Primolister/Farver/@i.GetValue("Item.Farvemuligheder.Option.Value")" /><br />@i.GetValue("Item.Farvemuligheder.Option.Label")</li>
                          }

                        }
                      </ul>

 

And the error message is now:

Line 56: The name 'i' does not exist in the current context

And I can't figure out what to write instead of @i.GetValue - there must be another approach to it?

Hope you can help me again - I have worked through all the other error messages and solved them, needs only this.

And by the way: Is the 

Item.Farvemuligheder.Option.IsSelected") != null

correct procedure for only showing the checked colours? I have a feeling, that it's not quite right... 

Thanks again - Lise

 
Thomas Schroll
Reply

Hi Lise

When you write '@foreach (var item in GetLoop("Item.Farvemuligheder.Options")) {' use 'item' instead of 'i'. Every option in the loop GetLoop("Item.Farvemuligheder.Options") is stored in 'item' for every time you go through the foreach loop.

When you use GetBoolean the result is either true or false. That mean that if GetBoolean("Item.Farvemuligheder.Option.IsSelected") is true do something. Therefore you need to remove '!=null' from the above.

Without knowing if all setup of items is correct, the c# code should be:

<ul>
                        @foreach (var item in GetLoop("Item.Farvemuligheder.Options")) {
                         
                          if(item.GetBoolean("Item.Farvemuligheder.Option.IsSelected")) {
                        <li style="text-align: left; width: 99px; height: 61px;"><img src="/Files/Billeder/Primolister/Farver/@item.GetValue("Item.Farvemuligheder.Option.Value")" /><br />@item.GetValue("Item.Farvemuligheder.Option.Label")</li>
                          }

                        }
                      </ul>

Regards Thomas

 
Lise T. Pedersen
Reply

Luckily, I'm understanding a little bit more for every time I get help! It works! Thank You!

 

You must be logged in to post in the forum