Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Apr 24, 2012 @ 11:30
    Anthony Candaele
    0

    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><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

  • Kasper 31 posts 52 karma points
    Apr 24, 2012 @ 13:04
    Kasper
    0

    Hi Anthony,

    You cannot use a switch statement directly on the dropdown list. You have to switch on the selected value. 

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 24, 2012 @ 14:14
    Anthony Candaele
    0

    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?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 24, 2012 @ 14:52
    Tom Fulton
    0

    Hi Anthony,

    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

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 24, 2012 @ 16:35
    Anthony Candaele
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft