Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi,
I'm trying to filter on a documenttype property using a @switch statement in Razor
The documenttype property 'memberCategory' is a custom datatype of type 'dropdown list'
My Razor code looks like this:
@foreach (var item in @Model.Member) { switch(@item.memberCategory) { case "Professor": <h2>Professor</h2> <ul class="nav"> <li><a href="@item.Url">@item.Name</a></li> </ul> break; default: break; } }
however when I try to save this in the Umbraco backend, I get the following error message:
A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type
Can somebody tell me what I am doing wrong?
Thanks for your help,Anthony
Hi Anthony,
You cannot use a switch statement directly on the dropdown list. You have to switch on the selected value.
Hi Kasper,
Thanks for your help. So I guess I have to first get the prevalues of my dropdown list with the getPrevalues(id) method. Is there an example of how to loop through the values of a dropdown list datatype with Razor?
Something like this should work:
string memberCategory = Model.memberCategory;switch (memberCategory) {...}
Guessing it's giving that error because of some type of dynamics going on.
Also to answer your other question about looping through prevalues (though not sure you need to now), check this blog post.
-Tom
Hi Tom,
Thanks for your help. I tried your code and although I could save the script, I got no result.
Then I changed the the line
string memberCategory = Model.memberCategory;
to
var memberCategory = Model.memberCategory;
This gave me the same errormessage:
"A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type"
Which was an indication it had something to do with the type of the variable
So then I changed my code to this:
@foreach (var item in @Model.Member)
{
string memberCategory = @item.memberCategory;
switch(memberCategory)
case "Professor":
<h2>Professor</h2>
<ul class="nav">
<li><a href="@item.Url">@item.Name</a></li>
</ul>
break;
default:
}
And now the script works!
I wonder if there is a way to debug Razor files :)
greetings,
Anthony
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
issue with @switch statement in Razor
Hi,
I'm trying to filter on a documenttype property using a @switch statement in Razor
The documenttype property 'memberCategory' is a custom datatype of type 'dropdown list'
My Razor code looks like this:
@foreach (var item in @Model.Member)
{
switch(@item.memberCategory)
{
case "Professor":
<h2>Professor</h2>
<ul class="nav">
<li><a href="@item.Url">@item.Name</a></li>
</ul>
break;
default:
break;
}
}
however when I try to save this in the Umbraco backend, I get the following error message:
A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type
Can somebody tell me what I am doing wrong?
Thanks for your help,
Anthony
Hi Anthony,
You cannot use a switch statement directly on the dropdown list. You have to switch on the selected value.
Hi Kasper,
Thanks for your help. So I guess I have to first get the prevalues of my dropdown list with the getPrevalues(id) method. Is there an example of how to loop through the values of a dropdown list datatype with Razor?
Hi Anthony,
Something like this should work:
Guessing it's giving that error because of some type of dynamics going on.
Also to answer your other question about looping through prevalues (though not sure you need to now), check this blog post.
-Tom
Hi Tom,
Thanks for your help. I tried your code and although I could save the script, I got no result.
Then I changed the the line
string memberCategory = Model.memberCategory;
to
var memberCategory = Model.memberCategory;
This gave me the same errormessage:
"A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type"
Which was an indication it had something to do with the type of the variable
So then I changed my code to this:
@foreach (var item in @Model.Member)
{
string memberCategory = @item.memberCategory;
switch(memberCategory)
{
case "Professor":
<h2>Professor</h2>
<ul class="nav">
<li><a href="@item.Url">@item.Name</a></li>
</ul>
break;
default:
break;
}
}
And now the script works!
I wonder if there is a way to debug Razor files :)
greetings,
Anthony
is working on a reply...