Help adding switch value inside C# razor

Hi all

In short, I’m trying to convert an existing if else statement into a switch case which is used within a C# razor view.

Depending on the switch case, different icons and colors are displayed within the foreach loop displaying a list of different icons and colors.

The problem is, I’m not sure how to add the condition to the switch value from the old if else below:

 if (node.Data.Property_Taxonomy.Contains("0/2/2437/2446")){
                  noticeicon = "glass";
                  noticecolor = "red";
} else if (node.Data.Property_Taxonomy.Contains("0/2/2437/2447")) {
                  noticeicon = "music";
                  noticecolor = "green";
} else if ....

What is the correct way to add the if condition into the switch value?

I did try the string value below without success:

@{
IExtendedOrderedQuery nQuery = Query.Where("Property_ID")
.IsEqualTo("12345");
ReadOnlyCollection<ContentNode> notices = new NodeFinder().Find(nQuery);
 
var nodes = new List<ContentNode>();
nodes = notices.ToList();

string value = "node.Data.Property_Taxonomy.Contains(???)"

foreach(ContentNode node in nodes) {
switch(value) {
                 
                case "0/2/2437/2446":
                    sIcon="glass";
                    sColor="sliver";
                    break;
                case "0/2/2437/2448":
                    sIcon="heart";
                    sColor="red";
                    break;
                case "0/2/2437/2449":
                    sIcon="star";
                    sColor="yellow";
                    break;
                default:
                    sIcon="no icon";
                    sColor="transparent";
                    break;
                }
             
                <p>
                   @sIcon<br>
                   @sColor
                </p>
 
              }
          }

Any ideas?

Thanks, barry

In short, if you need to use .Contains, you can’t. Switch statements are equals expressions, they cannot mimic .Contains

The only possible way to approach this using a switch statement is to make sure the input to the switch only contains the data you want to check against. That means the full value of node.Data.Property_Taxonomy can only contain 0/2/2437/2446 or the other variations you have within it. Or you must utilize node.Data.Property_Taxonomy to set a new variable to only the values the switch statement wants to receive.

I did think this might be the issue, been reading a few google posts - people reverting back to if else :neutral_face:

I think I understand. Sounds like this could work?

The taxonomy in discussion only has values of this sort, the format 0/2/2437/2446 will always be like this with a variation for each item.

Could you show example of how this could potentially work?
Or this will not work, best stick with if else?

Updated query above in #1 if makes any difference.


Also wondering below, when I was using the if else statements I tried to add a variable to reduce DRY, again, this didn’t work. What is the correct way to add/use a variable in this way?

var x = "node.Data.Property_Taxonomy.Contains";
if (@x("0/2/2437/2446"))

Thanks, Barry

Your variable approach would be

var x = node.Data.Property_Taxonomy;
if (x.Contains("0/2/2437/2446"))

You must have more data than just “0/2/2437/2446” in that property because otherwise the switch statement would work. Can you output the entire property value or set a breakpoint on it in a debugger to see what the true value is?

Correct the taxonomy is called Icons with a number of children, the icons:

Taxonomy Node Name
Node Key : 0/2/2437
Node Name : Icons
 
Taxonomy Node Name (child)
Node Key : 0/2/2437/2448
Node Name : Heart
StructuredContent (for reference)
-Icons
-- Glass
-- Heart
-- Music
-- Star

Can you output the entire property value or set a breakpoint on it in a debugger to see what the true value is?

A little tricky as I’m working in a CMS, the razor views are included files not sure how to get the values except what is show above, which is taken from the taxonomy GUI


Regarding the variable…

I did try this in two slightly different instances both producing errors:

Error: Cannot invoke a non-delegate type

and

error CS0118: ‘x’ is a ‘variable’ but is used like a ‘method’

Thanks, Barry

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.