Developer forum

Forum » Templates » If statement - item field - checkbox - probs

If statement - item field - checkbox - probs

Roop K. Rusbjerg
Reply

Hello,

I'm having issues with this peace of code, it has worked for me before but for reason it will not coorperate this time.

          @if (GetString("Item.AfdHellerup") == "True") {
          <strong>@i.GetValue("ItemPublisher:Item.AfdHellerup.Name")</strong>
          }

          @if (GetString("Item.AfdOdense") == "True") {
          <strong>@i.GetValue("ItemPublisher:Item.AfdOdense.Name")</strong>
          }

          @if (GetString("Item.AfdSkorping") == "True") {
          <strong>@i.GetValue("ItemPublisher:Item.AfdSkorping.Name")</strong>
          }

          @if (GetString("Item.AfdAarhus") == "True") {
          <strong>@i.GetValue("ItemPublisher:Item.AfdAarhus.Name")</strong>
          } 

          @if (GetString("Item.AfdMR_Aarhus") == "True") {
          <strong>@i.GetValue("ItemPublisher:Item.AfdMR_Aarhus.Name")</strong>
          }

          @if (GetString("Item.AfdViborg") == "True") {
          <strong>@i.GetValue("ItemPublisher:Item.AfdViborg.Name")</strong>
          }

It's a simple checkbox field type and I only want the ones that are checked to be shown. 

Best wishes,

Roop


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Roop,

 

Have you tried to put @TemplateTags() to see what the value of those tags are? I believe you may need to get the .Value for that field.

 

Alternatively I'd use GetBoolean instead of GetString. That way you don't need to parse values into string to get a boolean comparision, Example

@if (GetBoolean("Item.AfdViborg.Value")) {
 
    <strong>@i.GetValue("ItemPublisher:Item.AfdViborg.Name")</strong>
}

 

Best Regards,

Nuno Aguiar

 
Roop K. Rusbjerg
Reply

Hello Nuno,

The @TemplateTags() does'nt help, it won't show any of these values or tags.

The whole thing is part of a loop maybe that's the issue here?

I tried the GetBoolean you suggested, that didn't work either.

Is there anything else I can try or should I change the itemtype to a checkbox list instead? and use the foreach (loopitem...)

Best wishes,

Roop

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Roop,

 

If it's part of the loop, then that's the problem. You should have something like this

 

foreach( var field in GetLoop("Item.Fields") {

  ...

}

 

meaning your result needs to be

foreach( var field in GetLoop("Item.Fields") {

  field.TemplateTags()

  field.GetString(...)

}

 

(this is not actual code - validate it in your IDE)

 

Nuno Aguiar

 
Roop K. Rusbjerg
Reply

Hello again,

I got it to work with a checklist instead.

I tried your way but I don't think I got it completely right, as I kept getting errors.

But it seems to work well with the checklist item field.

Best regards,

Roop 

 

You must be logged in to post in the forum